免费代理ip爬虫分享
分享一个某代理网站的免费代理ip的爬虫,直接复制到pycharm运行就可以了。
注意:爬取的代理ip有点坑,因为是免费的所以过期时间很快,可能1分钟后就会失效。并且在scrapy使用这些代理ip还会给你打印一堆广告。且用且珍惜。
import requests
from lxml import etree
import json class XiciProxiesSpider(object): def __init__(self):
self.start_url = 'http://www.xicidaili.com/nn'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36'
} def get_page_from_url(self, url):
response = requests.get(url,headers=self.headers)
return response.content.decode() def get_data_from_page(self, page):
# print(page)
# 把page转换为Element对象
html = etree.HTML(page)
# 获取包含代理信息的tr列表
trs = html.xpath('//*[@id="ip_list"]/tr')[1:] # 遍历trs, 获取数据信息 data = {
'http':[],
'https':[]
} for tr in trs:
try:
ip = tr.xpath('./td[2]/text()')[0]
port = tr.xpath('./td[3]/text()')[0]
ip_type = tr.xpath('./td[6]/text()')[0].lower()
# 如果ip不是http或https直接返回
if ip_type not in data.keys():
return
# 构建代理数据
item = {ip_type: '{}://{}:{}'.format(ip_type, ip, port)}
# 检查代理IP是否可用, 如果可用添加到列表中
if self.validate_ip(item, ip_type):
print(item[ip_type])
data[ip_type].append(item[ip_type])
except Exception as ex:
print(ex)
print(etree.tostring(tr)) # print(data)
return data def validate_ip(self, item, ip_type): try:
test_url = "{}://baidu.com".format(ip_type)
response = requests.get('http://baidu.com', proxies=item, timeout=2)
if response.status_code == 200:
return True
return False
except Exception as ex:
return False def save_data(self, data):
with open('proxies.json', 'w') as f:
json.dump(data, f, indent=2) def run(self):
# 获取页面内宽容
page = self.get_page_from_url(self.start_url)
# 获取可用代理IP
data = self.get_data_from_page(page)
# 保存数据
self.save_data(data) if __name__ == '__main__':
fps = XiciProxiesSpider()
fps.run()
免费代理ip爬虫分享的更多相关文章
- Python3.x:免费代理ip的批量获取并入库
Python3.x:免费代理ip的批量获取并入库 一.简介 网络爬虫的世界,向来都是一场精彩的攻防战.现在许多网站的反爬虫机制在不断的完善,其中最令人头疼的,莫过于直接封锁你的ip.但是道高一尺魔高一 ...
- golang爬取免费代理IP
golang爬取免费的代理IP,并验证代理IP是否可用 这里选择爬取西刺的免费代理Ip,并且只爬取了一页,爬取的时候不设置useAgent西刺不会给你数据,西刺也做反爬虫处理了,所以小心你的IP被封掉 ...
- fastapi+vue搭建免费代理IP网站部署至heroku
说明 最近需要用到一些HTTP的代理,用于爬虫去爬取信息,搜索了一些网站,貌似现在这类提供免费代理IP的网站很多,刚好最近看了点vue的视频,弄个网站练练. 部署到heroku,预览地址:点击这里 F ...
- 码农代理免费代理ip端口字段js加密破解
起因 之前挖过爬取免费代理ip的坑,一个比较帅的同事热心发我有免费代理ip的网站,遂研究了下:https://proxy.coderbusy.com/. 解密 因为之前爬过类似的网站有了些经验,大概知 ...
- 常见的User-Agent及免费代理IP网站
常见的User-Agent 1.Android Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 ...
- 免费代理IP地址列表
代理地址最后验证日期:2017-8-28 纯真 66免费代理网 #推荐 西刺免费代理IP 酷伯伯HTTP代理 快代理 proxy360.cn 站大爷 Free Proxy List 年少#不稳定 全网 ...
- 采集15个代理IP网站,打造免费代理IP池
采集的站点: 免费代理IP http://ip.yqie.com/ipproxy.htm66免费代理网 http://www.66ip.cn/89免费代理 http://www.89ip.cn/无忧代 ...
- C#多线程爬虫抓取免费代理IP
这里用到一个HTML解析辅助类:HtmlAgilityPack,如果没有网上找一个增加到库里,这个插件有很多版本,如果你开发环境是使用VS2005就2.0的类库,VS2010就使用4.0,以此类推.. ...
- 简单爬虫-爬取免费代理ip
环境:python3.6 主要用到模块:requests,PyQuery 代码比较简单,不做过多解释了 #!usr/bin/python # -*- coding: utf-8 -*- import ...
随机推荐
- leetcode 66.加一 python3
class Solution: def plusOne(self, digits): """ :type digits: List[int] :rtype: List[i ...
- LeetCode数组查找问题
简单的二分查找法:(Leetcode704): 注意条件(low<=high) public int search(int[] nums, int target) { int low=0; in ...
- Linux 链路聚合
Linux 链路聚合 链路聚合与双网卡绑定几乎相同,可以实现多网卡绑定主从荣誉,负载均衡,提高网络访问流量.但链路聚合与双网卡绑定技术(bond)不同点就在于,双网卡绑定只能使用两个网卡绑定,而链路聚 ...
- Git 版本还原命令
转载:https://blog.csdn.net/yxlshk/article/details/79944535 1.需求场景: 在利用github实现多人协作开发项目的过程中,有时会出现错误提交的情 ...
- kali网络配置
touch 1.txt#创建一个文件 配置网卡 auto eth0iface eth0 inet staticaddress 172.16.30.102#要设置的主机IP地址netmask 255.2 ...
- 【做题】ZJOI2017仙人掌——组合计数
原文链接 https://www.cnblogs.com/cly-none/p/ZJOI2017cactus.html 给出一个\(n\)个点\(m\)条边的无向连通图,求有多少种加边方案,使得加完后 ...
- [Redis] - redis实战1
rememberMe>>>>:null Creating a new SqlSession SqlSession [org.apache.ibatis.session.defa ...
- Bytom BIP-32协议和BIP-44协议
我们知道HD(分层确定性)钱包,基于 BIP-32:多币种和多帐户钱包,基于 BIP-44:最近比原社区的钱包开发者对比原的BIP-32和BIP-44协议有疑问,所以我今天就专门整理了一下该协议的内容 ...
- structure streaming笔记
基于micro-batch, spark2.3之后, 支持continues processing 基于spark SQL 如同在静态table上运行标准批查询一样表现流计算, spark 通过在一个 ...
- 用 jupyter notebook 打开 oui.txt 文件出现的问题及解决方案
问题背景:下载了2018 IEEE 最新的 oui.txt 文件.里面包含了 设备 MAC 地址的前六位对应的厂商.要做的工作是,将海量设备的 MAC 地址与 oui.txt 文件的信息比对,统计出 ...