|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests
if __name__ == "__main__":
headers = {
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81'
}
url = 'https://www.baidu.com/s?'
kw = input('enter a word:')
param = {
'wd':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 fp:
fp.write(page_text)
print(fileName,'保存成功!!!')
#跑程序之后出现这样的报错
enter a word:小甲鱼
Traceback (most recent call last):
File "E:/python_game/爬取研究生.py", line 12, in <module>
response = requests.get(url = url, params = param,headers = headers)
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 528, in request
prep = self.prepare_request(req)
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 456, in prepare_request
p.prepare(
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 317, in prepare
self.prepare_headers(headers)
File "C:\Users\westbrook\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 449, in prepare_headers
for header in headers.items():
AttributeError: 'set' object has no attribute 'items'
有点混乱,请问大佬这是哪出问题了呢?
headers = {'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81'
}
|
|