通过输入指定的关键字搜索网页中的信息,啥也不说直接上代码
import requests
def spider():
headers= {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
}
url = "https://www.sogou.com/web"
kw = input('enter a word')
param = {
"query": kw
}
response = requests.get(url=url, params=param, headers= headers)
page_text = response.text
fileName= kw+'.html'
with open(fileName, 'w', encoding='UTF-8') as f:
f.write(page_text)
print(fileName,"保存成功!!")
if __name__ == '__main__':
spider()