测试当前访问百度的IP地址(代理IP)
import requests
from lxml import etree
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3521.2 Safari/537.36'
}
url = 'https://www.baidu.com/s?wd=ip'
# 代理IP的参数是proxies
res_text = requests.get(url=url,headers=headers,proxies={'https':'115.171.85.70:9000'}).text
tree = etree.HTML(res_text)
my_ip_list = tree.xpath('//*[@id="1"]/div[1]/div[1]/div[2]//text()')
my_ip = ''.join(my_ip_list).strip().replace('\n','').replace('\t','')
print(my_ip)