【简单爬虫】opener.addheaders()出错
代码importurllib.request as u
url='http://www.whatismyip.com.tw'
pro=u.ProxyHandler({'http':'47.106.105.236:80'})
opener=u.build_opener(pro)
opener.addheaders[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77')]
u.install_opener(opener)
response=u.urlopen(url)
html=response.read().decode("utf-8")
print(html)
错误
Traceback (most recent call last):
File "D:\py\代理ip测验.py", line 7, in <module>
opener.addheaders[('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77')]
TypeError: list indices must be integers or slices, not tuple
大佬们能看看是哪里的问题吗? importurllib.request as u
url='http://www.whatismyip.com.tw'
pro=u.ProxyHandler({'https':'47.106.105.236:80'})
opener=u.build_opener(pro)
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77')]
u.install_opener(opener)
response=u.urlopen(url)
html=response.read().decode("utf-8")
print(html)
临时号 发表于 2022-8-2 20:59
{:10_297:} 牛
页:
[1]