hello? 发表于 2022-8-2 20:40:27

【简单爬虫】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
大佬们能看看是哪里的问题吗?

临时号 发表于 2022-8-2 20:59:29

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)

hello? 发表于 2022-8-2 21:21:58

临时号 发表于 2022-8-2 20:59


{:10_297:}

GEO-Shao 发表于 2022-8-5 18:08:41

python0729 发表于 2022-8-26 20:49:39

页: [1]
查看完整版本: 【简单爬虫】opener.addheaders()出错