1. 程式人生 > 其它 >python爬蟲筆記(4-1)bs4模組

python爬蟲筆記(4-1)bs4模組

參考:https://www.jianshu.com/p/9254bdc467b2 說明: Beautiful Soup 4.4.0 文件: https://beautifulsoup.readthedocs.io/zh_CN/latest/ 它的作用是能夠簡單快速地提取網頁中指定的內容,用requests模組獲取網頁原始碼,用bs4的介面將網頁原始碼生成一個物件,通過這個物件的方法來提取資料 bs4解析的邏輯: <標籤 屬性=“值”>被標記的內容</標籤> 通過某個標籤,獲取標籤特徵(屬性),通過特徵定位到要提取的內容 1、安裝bs4模組: pip3 install Beautifulsoup4 2、匯入 from bs4 import BeautifulSoup 3、從bs物件中查詢資料 3.1、find方法:查一個,取第一個的值 find(‘標籤’,屬性=‘值’) soup.find('a') soup.find('a', class_='xxx') soup.find('a', title='xxx') soup.find('a', id='xxx') soup.find('a', id=re.compile(r'xxx')) 3.2、find_all方法:查所有,返回一個列表 find_all(‘標籤’,屬性=‘值’) soup.find_all('a') soup.find_all('a', class_='wang') soup.find_all('a', id=re.compile(r'xxx')) soup.find_all('a', limit=2) 提取出前兩個符合要求的a