python如何使用代理

做网络爬虫,更方便快捷地获取更多的信息的时候,不可避免的会使用到代理,以下是Python使用代理的调用方法

下面我用(http://www.xiongmaodaili.com/)来做个例子。

    # 只需要替换14行和15行的orderno和secret即可运行

import sys
import time
import hashlib
import requests
from lxml import etree

_version = sys.version_info

is_python3 = (_version[0] == 3)

# 个人中心获取orderno与secret
orderno = "DT20179xxxxxxxxx" 
secret = "3f9c2ecac7xxxxxxxxxxxxxxxx"

ip = "dynamic.xiongmaodaili.com"
port = "8088"

ip_port = ip + ":" + port

timestamp = str(int(time.time())) # 计算时间戳
txt = ""
txt = "orderno=" + orderno + "," + "secret=" + secret + "," + "timestamp=" + timestamp

if is_python3:
txt = txt.encode()

md5_string = hashlib.md5(txt).hexdigest() # 计算sign
sign = md5_string.upper() # 转换成大写
print(sign)
auth = "sign=" + sign + "&" + "orderno=" + orderno + "&" + "timestamp=" + timestamp

print(auth)
proxy = {"http": "http://" + ip_port, "https": "https://" + ip_port}
headers = {"Proxy-Authorization": auth}
r = requests.get("https://www.baidu.com", headers=headers, proxies=proxy, verify=False,allow_redirects=False)
print(r.status_code)
print(r.content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值