安装好后,输入
import requests
from bs4 import BeautifulSoup
F5运行如果不报错则说明安装成功。
打开浏览器,输入'www.baidu.com'
,即进入百度,随便搜索什么,我这里用'python'
为例
可以发现,百度搜索出来的链接为
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=python****
最后可以简化为:
https://www.baidu.com/s?wd=python
所以首先尝试获取搜索结果的html:
import requests
from bs4 import BeautifulSoup
url=‘https://www.baidu.com/s?wd=’+‘python’
headers = {“Accept”:“text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9”,“User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.16 Safari/537.36”}
html = requests.get(url,headers=headers).text
print(html)
<