赞
踩
- import requests
- from lxml import etree
- headers = {
- 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36',
- }
- url = 'https://www.sogou.com/web?query=ip'
- #使用代理服务器发起请求
- #proxies={'代理类型':'ip:port'}
- page_text = requests.get(url=url,headers=headers,proxies={'https':'42.57.150.150:4278'}).text
- tree = etree.HTML(page_text)
- data = tree.xpath('//*[@id="ipsearchresult"]/strong/text()')[0]
- print(data)
打开芝麻代理,注意这里有实名认证,不可恶意使用代理去攻击别人的网站,即通过代理也是可溯源的:API获取-芝麻HTTP代理
- from bs4 import BeautifulSoup
- from lxml import etree
- import requests
- import time
- import random
- headers = {
- 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
- }
- #构建一个代理池,通过芝麻代理获取代理
- proxy_url = 'http://webapi.http.zhimacangku.com/getip?num=10&type=2&pro=&city=0&yys=0&port=1&pack=222763&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1®ions='
- json_data = requests.get(url=proxy_url,headers=headers).json()
- json_list = json_data['data']
- proxy_list = [] #代理池,每次请求,可以随机从代理池中选择一个代理来用
- for dic in json_list:
- ip = dic['ip']
- port = dic['port']
- n_dic = {
- 'https':ip+':'+str(port)
- }
- proxy_list.append(n_dic)
-
- url = 'https://www.sogou.com/web?query=ip'
- page_text = requests.get(url=url,headers=headers,proxies=random.choice(proxy_list)).text
- tree = etree.HTML(page_text)
- data = tree.xpath('//*[@id="ipsearchresult"]/strong/text()')[0]
- print(data)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。