#实现一个简易的在线网页采集器#
import requests
#爬取网站页面数据(仅学习使用)
if __name__=="__main__":
#UA伪装:让爬虫对应的请求载体身份标识伪装成为一款浏览器
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
#指定的url
url ='/web'
#处理url携带的参数:封装到字典中
kw = input('enter a word')
param = {
'query':kw
}
#对指定的url发起请求对应的url是携带参数的,并且请求中处理了参数
response = requests.get(url=url,params=param,headers=headers)
page_text = response.text
fileName = kw + '.html'
with open(fileName,'w',encoding='utf-8') as fp:
fp.write(page_text)
print(fileName,'保存成功!!!!')