简易网页采集器想法及思想:
1.导入requests库
2.给出对应网址
3.参数用字典给出
4.UA伪装封装headers
5.持久化存储到文件中
import requests
url="https://www.sogou.com/web"
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.0.3161 SLBChan/29"
}
word=input("请输入搜索内容")
param={
"query":word
}
response=requests.get(url=url,params=param,headers=headers)
context=response.text
filename=word+".html"
with open(filename,"w",encoding="utf-8") as fp:
fp.write(context)