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 ...
随机推荐
- [React Native] Dismiss the Keyboard in React Native
In this lesson you will learn how create a re-usable component that gives the user the ability to di ...
- linux搜索文件过程
1.文件里的数据是放在磁盘的数据区中的,而一个文件名称则是通过相应的i节点与这些磁盘块联系起来.这些盘块的号码就存放在i节点的逻辑块数组i_zone[]中.在文件系统的一个文件夹中,当中全部文件名称信 ...
- POJ 2442 Sequence(堆的使用练习)
题目地址:id=2442">POJ 2442 真心没想到这题的思路. .原来是从第一行逐步向下加,每次都仅仅保存前n小的数.顺便练习了下堆.. 只是感觉堆的这样的使用方法用的不太多啊. ...
- iOS开发- Images can’t contain alpha channels or transparencies 解决的方法
新版的iTunesConnect真是各种问题阿... 蛋疼要命. 上传介绍图片(屏幕截图)的时候 , 遇到了这个问题 Images can't contain alpha channels or tr ...
- 9、包、访问控制、import、static、static代码块、final、抽象类、接口、instanceof、多态
1.包 三级命名:公司的尾缀(com).公司名字(baidu).业务(Sale) java.lang:默认包:String.Math,Object,System java.util:工具包 java. ...
- POJ 1012 Joseph(打表题)
题意:约瑟夫环的变形.要求寻找到一个杀人循环节m使后半节的坏人先被所有杀光. 直接链表模拟出结果,再打表即可: 代码:(凝视的是打表码) #include<iostream> #inclu ...
- CG 内置函数 和 HLSL 内置函数
CG 内置函数 英伟达官网链接: http://http.developer.nvidia.com/Cg/index_stdlib.html absacosallanyasinatan2atanbi ...
- List of content management systems
https://en.wikipedia.org/wiki/List_of_content_management_systems Microsoft ASP.NET Name Platform Sup ...
- python中对单例模式的理解
class Foo(object): instance = None def __init__(self): pass def process(self): ' @classmethod #版本1单例 ...
- fullpage中大的图片超过一屏怎么在手机端滑动显示?
fullpage中大的图片超过一屏怎么在手机端滑动显示?(设置overflow电脑端是会出现滚动条的,但是在手机端不出现滚动条,图片也不可左右滑动显示) var $window = $(window) ...