先在网页进行搜索
得到url
https://www.sogou.com/web?query=波晓张&_asf=www.sogou.com&_ast=&w=01019900&p=40040100&ie=utf8&from=index-nologin&s_from=index&sut=9915&sst0=1645748279699&lkt=0%2C0%2C0&sugsuv=1645702898231618&sugtime=1645748279699
提取主要部分的url
https://www.sogou.com/web?query=波晓张
编写代码
# coding=UTF-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import requests
if __name__ == '__main__':
#UA伪装:将对应的User-Agent封装到一个字典中
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:97.0) Gecko/20100101 Firefox/97.0'
}
#处理url携带的参数:封装到字典中
url='https://www.sogou.com/web?'
kw=raw_input('你想要爬取什么:')
param={
'query':kw
}
#对指定的urL发起的请求对应的url是携带参数的,并且请求过程中处理了参数
response = requests.get(url=url,params=param,headers=headers)
page_text = response.text
fileName = kw+'.html'
with open(fileName.decode('utf-8'),'w') as fp:
fp.write(page_text)
print (fileName,'保存成功!!!')