Python中BeautifulSoup库的find_all、select用法等
python中Bs4这个包是用来解析网页源码的包,爬虫程序常用这个包解析爬取网页源码进行分析,今天主要介绍这个包的一些基本使用。自行安装
可以直接查看官方文档: https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/
创建beautifulsoup对象
from bs4 import BeautifulSoup
import requests
headers = {
'Referer': 'https://hz.qk365.com/list/',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
base_url = 'https://www.baidu.com' # 这个是你要爬的网址
response = requests.get(url=base_url, headers=headers)
# print(response.text)
soup = BeautifulSoup(html_str, 'lxml')
创建完成可以自行打印内容和tpye看看