> For the complete documentation index, see [llms.txt](https://clu.gitbook.io/python-web-crawler-note/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clu.gitbook.io/python-web-crawler-note/22-beautifulsoupshuo-ming.md).

# 2.2 BeautifulSoup說明

接續前一篇的範例, 首先是取得response的方法:

```python
resp = requests.get(url)
```

這是走HTTP GET的方式, POST的使用方式之後會看到.

取得response後, 透過parser轉換成BeautifulSoup比較好處理的格式:

```
soup = BeautifulSoup(resp.text, 'html.parser')
```

soup是獲得的文件物件, html.parser是支援的parser.

關於parser, 比較常見的parser有這些:

* html.parser
* html5lib
* lxml

BeautifulSoup會根據指定的parser把文件轉換成一個複雜的樹形結構, 每個節點都是Python物件, 所有物件大致上可歸類為4種:

* Tag: 基本上就是HTML裡的各種tag(div, p, etc...). 每個tag有兩個重要的屬性: **name**和**attrs**.
  * name: 即tag的名字或著tag本身的name.
  * attrs: 通常指一個tag的class(常用).
* NavigableString: 取得tag內的文字, 如:&#x20;

  ```
  soup.p.string
  ```
* BeautifulSoup: 表示一個文件的所有內容.
* Comment: 此物件是一個特殊的NavigableString物件, 其輸出的內容不包含註解符號.
