前言
我正在进行代理ip的测试,但报了这么个错误:AttributeError: 'str' object has no attribute 'get'
过程简述
从“芝麻代理”获取代理ip,用这些代理ip访问百度,如果返回状态码200,就算成功
错误重现
import requests
# 测试地址,看看代理ip能不能正常访问百度
url = 'https://www.baidu.com/'
# 从芝麻代理获取的ip,有效期一天
ipProxy_url = 'http://webapi.http.zhimacangku.com/getip?num=5&type=1&pro=&city=0&yys=0&port=1&pack=218256&ts=0&ys=0&cs=0&lb=4&sb=0&pb=4&mr=1®ions='
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.36'
}
def get_ipProxy(url, headers):
"""获取代理ip"""
response = requests.get(url=url, headers=headers)
text = response.text
ipProxys = text.split('\n')[0:-1]
print(ipProxys)
return ipProxys
def testProxyIp(url, headers, ipProxys):
"""测试代理ip地址是否可用"""
for ip in ipProxys:
print(f'正在测试代理ip:{ip}')
response = requests.get(url=url, headers=headers, proxies=ip)
if response.status_code == 200:
print('该代理ip可用')
else:
print('该代理ip不可用')
if __name__ == '__main__':
ipProxys = get_ipProxy(url=ipProxy_url, headers=headers)
testProxyIp(url=url, headers=headers, ipProxys=ipProxys)
当我执行上面的代码后,报了这么个错误:AttributeError: 'str' object has no attribute 'get'
发生错误的原因
经过多次排查,确定是代理有问题,但当时的我还不知道具体问题是什么,怀疑是传入的代理格式有问题
最终,通过查阅文档,发现了问题
传入的proxies的格式应该是字典类型,而我传入的是字符串
根据官方文档的指引,最终解决问题
解决方法
import requests
# 测试地址,看看代理ip能不能正常访问百度
url = 'https://www.baidu.com/'
# 从芝麻代理获取的ip,有效期一天
ipProxy_url = 'http://webapi.http.zhimacangku.com/getip?num=5&type=1&pro=&city=0&yys=0&port=1&pack=218256&ts=0&ys=0&cs=0&lb=4&sb=0&pb=4&mr=1®ions='
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.36'
}
def get_ipProxy(url, headers):
"""获取代理ip"""
response = requests.get(url=url, headers=headers)
text = response.text
ipProxys = text.split('\n')[0:-1]
print(ipProxys)
return ipProxys
def testProxyIp(url, headers, ipProxys):
"""测试代理ip地址是否可用"""
for ip in ipProxys:
print(f'正在测试代理ip:{ip}')
# 传入的代理ip应该是字典类型,否则会报错:AttributeError: 'str' object has no attribute 'get'
proxies = {
'https://': f'{ip}',
'http://': f'{ip}'
}
try:
response = requests.get(url=url, headers=headers, proxies=proxies)
if response.status_code == 200:
print('该代理ip可用')
else:
print('该代理ip不可用')
except Exception as e:
print(f'出现异常情况:{e}')
if __name__ == '__main__':
ipProxys = get_ipProxy(url=ipProxy_url, headers=headers)
testProxyIp(url=url, headers=headers, ipProxys=ipProxys)
尾声
如果这篇文章对您有所帮助,那么这篇文章就有意义!文章来源:https://www.toymoban.com/news/detail-437935.html
感谢您的观看!文章来源地址https://www.toymoban.com/news/detail-437935.html
到了这里,关于Python 使用requests模块进行ip代理时报错:AttributeError: ‘str‘ object has no attribute ‘get‘的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!