购买的固定代理IP一般都需要账号密码, 在网上找了几个使用方法,但是都报错,所以,就想了这个笨办法,如有好办法希望大家指点。
import requests
from requests.auth import HTTPBasicAuth
proxy = {
'http': 'http://ip:port',
'https': 'https://ip:port8'
}
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/119.0.6045.123 Safari/537.36',
'Connection': 'keep-alive',
'Proxy-Authorization': '...' # 在浏览器中获取此值
}
p = requests.get('http://icanhazip.com', headers=head)
print(p.content.decode('utf-8'))
------------- 下面是获取Proxy-Authorization的方法-------------
首先,就是获取在headers 中的 Proxy-Authorization
用的是Chrome浏览器, 设置代理ip
然后点击 确定
打开一个新的网页, 随意登录ipip.net即可查询验证当前浏览器ip网络
在scrapy的中间件中添加:
1.request.meta['proxy'] = "http://ip:port"
2.# 添加Proxy-Authorization对应得值,添加在请求的headers中
3.request.headers['Proxy-Authorization'] = 'Basic ...........'
这样就将固定ip设置为代理了~
关于(Python 爬虫使用固定代理IP)的分享就介绍到这里,感谢您花时间阅读内容,想要了解更多信息可以上(h.shanchendaili.com)闪臣http咨询喔!