python爬虫批量抓取ip代理
使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁。
ip代理可以从这个网站获取:http://www.xicidaili.com/nn/。
因此写一个python程序来获取ip代理,保存到本地。
python版本:3.6.3
#grab ip proxies from xicidaili
import sys, time, re, requests
from multiprocessing.dummy import Pool as ThreadPool
from lxml import etree IP_POOL = 'ip_pool.py'
URL = 'http://www.xicidaili.com/nn/' #IP代理 高匿
#URL = 'http://www.xicidaili.com/wt/' #IP代理 http
RUN_TIME = time.strftime("%Y-%m-%d %H:%M", time.localtime()) #执行时间 #用字典存放有效ip代理
alive_ip = {'http': [], 'https': []}
#多线程
pool = ThreadPool(20) #返回html文本
def get_html(url):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate",
"Referer": "https://www.xicidaili.com/",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": ""
}
r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
return r.text #测试ip代理是否存活
def test_alive(proxy):
global alive_ip
proxies = {'http': proxy}
try:
r = requests.get('https://www.baidu.com', proxies=proxies, timeout=3)
if r.status_code == 200:
if proxy.startswith('https'):
alive_ip['https'].append(proxy)
else:
alive_ip['http'].append(proxy)
except:
print("%s无效!"%proxy) #解析html文本,获取ip代理
def get_alive_ip_address():
iplist = []
html = get_html(URL)
selector = etree.HTML(html)
table = selector.xpath('//table[@id="ip_list"]')[0]
lines = table.xpath('./tr')[1:]
for line in lines:
speed, connect_time = line.xpath('.//div/@title')
data = line.xpath('./td')
ip = data[1].xpath('./text()')[0]
port = data[2].xpath('./text()')[0]
anonymous = data[4].xpath('./text()')[0]
ip_type = data[5].xpath('./text()')[0]
#过滤掉速度慢和非高匿的ip代理
if float(speed[:-1])>1 or float(connect_time[:-1])>1 or anonymous != '高匿':
continue
iplist.append(ip_type.lower() + '://' + ip + ':' + port)
pool.map(test_alive, iplist) #把抓取到的有效ip代理写入到本地
def write_txt(output_file):
with open(output_file, 'w') as f:
f.write('#create time: %s\n\n' % RUN_TIME)
f.write('http_ip_pool = \\\n')
f.write(str(alive_ip['http']).replace(',', ',\n'))
f.write('\n\n')
with open(output_file, 'a') as f:
f.write('https_ip_pool = \\\n')
f.write(str(alive_ip['https']).replace(',', ',\n'))
print('write successful: %s' % output_file) def main():
get_alive_ip_address()
write_txt(output_file) if __name__ == '__main__':
try:
output_file = sys.argv[1] #第一个参数作为文件名
except:
output_file = IP_POOL
main()
运行程序:
root@c:test$ python get_ip_proxies.py
write successful: ip_pool.py
查看文件:
root@c:test$ vim ip_pool.py
#create time: 2019-03-14 19:53 http_ip_pool = \
['http://183.148.152.1:9999',
'http://112.85.165.234:9999',
'http://112.87.69.162:9999',
'http://111.77.197.10:9999',
'http://113.64.94.80:8118',
'http://61.184.109.33:61320',
'http://125.126.204.82:9999',
'http://125.126.218.8:9999',
'http://36.26.224.56:9999',
'http://123.162.168.192:40274',
'http://116.209.54.125:9999',
'http://183.148.148.211:9999',
'http://111.177.161.111:9999',
'http://116.209.58.245:9999',
'http://183.148.143.38:9999',
'http://116.209.55.218:9999',
'http://114.239.250.15:9999',
'http://116.209.54.109:9999',
'http://125.123.143.98:9999',
'http://183.6.130.6:8118',
'http://183.148.143.166:9999',
'http://125.126.203.228:9999',
'http://111.79.198.74:9999',
'http://116.209.53.215:9999',
'http://112.87.69.124:9999',
'http://112.80.198.13:8123',
'http://182.88.160.16:8123',
'http://116.209.56.24:9999',
'http://112.85.131.25:9999',
'http://116.209.52.234:9999',
'http://175.165.128.223:1133',
'http://122.4.47.199:8010',
'http://112.85.170.204:9999',
'http://49.86.178.206:9999',
'http://125.126.215.187:9999'] https_ip_pool = \
['https://183.148.156.98:9999',
'https://111.79.199.167:808',
'https://61.142.72.150:39894',
'https://119.254.94.71:42788',
'https://221.218.102.146:33323',
'https://122.193.246.29:9999',
'https://183.148.139.173:9999',
'https://60.184.194.157:3128',
'https://118.89.138.129:52699',
'https://112.87.71.67:9999',
'https://58.56.108.226:43296',
'https://182.207.232.135:50465',
'https://111.177.186.32:9999',
'https://58.210.133.98:32741',
'https://115.221.116.71:9999',
'https://183.148.140.191:9999',
'https://183.148.130.143:9999',
'https://116.209.54.84:9999',
'https://125.126.219.125:9999',
'https://112.85.167.158:9999',
'https://112.85.173.76:9999',
'https://60.173.244.133:41306',
'https://183.148.147.223:9999',
'https://116.209.53.68:9999',
'https://111.79.198.102:9999',
'https://123.188.5.11:1133',
'https://60.190.66.131:56882',
'https://112.85.168.140:9999',
'https://110.250.65.108:8118',
'https://221.208.39.160:8118',
'https://116.209.53.77:9999',
'https://116.209.58.29:9999',
'https://183.148.141.129:9999',
'https://124.89.33.59:53281',
'https://116.209.57.149:9999',
'https://58.62.238.150:32431',
'https://218.76.253.201:61408']
之后就可以直接使用了
from ip_pool import http_ip_pool, https_ip_pool
python爬虫批量抓取ip代理的更多相关文章
- from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)
使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium) - 北平吴彦祖 - 博客园 https://www.cnblogs.com/stevenshushu/p ...
- Python爬虫实战---抓取图书馆借阅信息
Python爬虫实战---抓取图书馆借阅信息 原创作品,引用请表明出处:Python爬虫实战---抓取图书馆借阅信息 前段时间在图书馆借了很多书,借得多了就容易忘记每本书的应还日期,老是担心自己会违约 ...
- python爬虫数据抓取方法汇总
概要:利用python进行web数据抓取方法和实现. 1.python进行网页数据抓取有两种方式:一种是直接依据url链接来拼接使用get方法得到内容,一种是构建post请求改变对应参数来获得web返 ...
- c#批量抓取免费代理并验证有效性
之前看到某公司的官网的文章的浏览量刷新一次网页就会增加一次,给人的感觉不太好,一个公司的官网给人如此直白的漏洞,我批量发起请求的时候发现页面打开都报错,100多人的公司的官网文章刷新一次你给我看这个, ...
- 【转】Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫:抓取新浪新闻数据
案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...
- Python爬虫实现抓取腾讯视频所有电影【实战必学】
2019-06-27 23:51:51 阅读数 407 收藏 更多 分类专栏: python爬虫 前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问 ...
- Python爬虫,抓取淘宝商品评论内容!
作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...
- 静听网+python爬虫+多线程+多进程+构建IP代理池
目标网站:静听网 网站url:http://www.audio699.com/ 目标文件:所有在线听的音频文件 附:我有个喜好就是听有声书,然而很多软件都是付费才能听,免费在线网站虽然能听,但是禁ip ...
随机推荐
- POJ - 3321 Apple Tree (线段树 + 建树 + 思维转换)
id=10486" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- 微信公众号开发将war包导入新浪sae出现错误
JAVA_Error: Error for /wechat.do java.lang.NoSuchFieldError: INSTANCE at org.apache.http.impl.io.Def ...
- YTUOJ-计算该日在本年中是第几天(用户自己定义类型)
题目描写叙述 定义一个结构体变量(包含年.月.日).编写一个函数days,由主函数将年.月.日传递给函数days,计算出该日在本年中是第几天并将结果传回主函数输出. 输入 年月日 输出 当年第几天 例 ...
- Eclipse中项目进行发布到Tomcat中的位置
Eclips配置tomcat默认是发布到.metadate\plugins\目录下的,wtpwebapps.这样在实际的tomcat目录下,就找不到发布的项目.我们可以自己进行设置,在控制栏中找到se ...
- Android Stuido 好卡怎么办?不要急,兄弟来教你
近期使用Android studio 开发app,编译的时候特别卡,常常卡死.我的机器 i3 + 8G,按道理来说流畅的跑个androidstudio还是绰绰有余的... 于是在各大论坛寻找解决方式, ...
- SPOJ1812: LCS2 - Longest Common Substring II & BZOJ2946: [Poi2000]公共串
[传送门:SPOJ1811&BZOJ2946] 简要题意: 给出若干个字符串,求出这些字符串的最长公共子串 题解: 后缀自动机 这两道题的区别只是在于一道给出了字符串个数,一个没给,不过也差不 ...
- Java类和对象5
写一个Java应用程序,该应用程序包括2个类:Print类和主类E.Print类里有一个方法output()功能是输出100 ~ 999之间的所有水仙花数(各位数字的立方和等于这个三位数本身,如: 3 ...
- 双列集合Map的嵌套遍历
双列集合Map的嵌套使用,例如HashMap中还有一个HashMap,这样的集合遍历起来稍微有点儿复杂.例如一个集合:HashMap<Integer,HashMap<String,Inte ...
- SSD-tensorflow-2 evaluation
测试就是用voc2007的test set来测试已经训练好的checkpoint的mAP,github上提供了三个已经训练好的model的checkpoint checkpoint 里面已有的300_ ...
- uptime---系统总共运行时间和系统的平均负载
uptime命令能够打印系统总共运行了多长时间和系统的平均负载.uptime命令可以显示的信息显示依次为:现在时间.系统已经运行了多长时间.目前有多少登陆用户.系统在过去的1分钟.5分钟和15分钟内的 ...