python调用requests时发生too many values to unpack (expected 2)错误
问题遇到的现象和发生背景
编程时调用requests发生错误
错误代码
def mainf():
headerses = {"User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"}
urls='http://www.centrechina.com/news/jiaodian/'
response = requests.get(urls,headerses)
html_data=response.text
soupClass=BeautifulSoup(html_data,"lxml")
news_url_get=[]
a=soupClass.select('.ajax-load-con h2 a') #选中a标签
for i,x in a:
news_url_get.append(i['href']) #提取标签并加到列表中
print(news_url_get)
mainf()
运行结果及报错内容
pycharm:
Traceback (most recent call last):
File "G:\python_project\test_requests.py", line 42, in <module>
today_news()
File "G:\python_project\test_requests.py", line 32, in today_news
response = requests.get(url,headers)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py", line 573, in request
prep = self.prepare_request(req)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\sessions.py", line 484, in prepare_request
p.prepare(
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 368, in prepare
self.prepare_url(url, params)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 474, in prepare_url
enc_params = self._encode_params(params)
File "C:\Users\David Chen\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 121, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)
vscode:
我想要达到的结果
正确输出爬取内容