本文主要代码用于有代理网站http://www.kuaidaili.com/free/intr中的代理ip爬取,爬虫使用过程中需要输入含有代理ip的网页链接。

测试ip是否可以用

import telnetlib
import requests
from bs4 import BeautifulSoup, element
import json
def filter_ip(ip_info):
'''
判断给定代理ip是否可用
:param ip_info:
:return:
'''
ip, port = ip_info.split(':')
try:
telnetlib.Telnet(ip, port=int(port), timeout=5)
except:
return False
else:
return True

获取指定页面的代理ip地址

def get_ip(url):
'''
爬取url中的全部代理ip
:param url: 存在代理ip的网页
:return:
'''
headers = {
'Host': 'www.kuaidaili.com',
'User-Agent': 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
'Accept-Encoding': 'gzip, deflate',
'Referer': 'http://www.youdaili.net/',
'Cookie': 'yd_cookie=f79e58ee-56a0-420270b11723b478602ef5c39e38dfb62a0f; _ydclearance=7c74cfd9ee4bc1537da1e0c6-b5de-458f-bc6c-6c973e273296-1505809848; channelid=0; sid=1505801661396785; _ga=GA1.2.1178656966.1505802632; _gid=GA1.2.1266088947.1505802632; Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1505802632; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=1505802650',
'Connection': 'keep-alive',
} wb_data = requests.get(url, headers=headers)
soup = BeautifulSoup(wb_data.text, 'html5lib')
ip_text = soup.select('#list > table > tbody > tr > td:nth-of-type(1)')
port_text = soup.select('#list > table > tbody > tr > td:nth-of-type(2)')
ip = [e.text for e in ip_text if len(e.text) >6]
port = [e.text for e in port_text if len(e.text) > 0] ip_port =['{0}:{1}'.format(a, b) for a, b in zip(ip, port)]
ip_good = [e for e in ip_port if filter_ip(e)]
return ip_good

调用爬虫

if __name__ == '__main__':
# 包含ip的网页链接
urls = ['http://www.kuaidaili.com/free/intr/{0}/'.format(i) for i in range(1, 21)] ip_good = list()
for url in urls:
tmp = get_ip(url)
ip_good.extend(tmp)
print url
print ip_good
with open('./ip.txt', 'w') as f:
f.write(json.dumps(ip_good))
f.close()

使用Python爬取代理ip的更多相关文章

  1. Python爬取代理ip

    # -*- coding:utf-8 -*- #author : willowj import urllib import urllib2 from bs4 import BeautifulSoup ...

  2. python爬虫爬取代理IP

    # #author:wuhao # #--*------------*-- #-****#爬取代理IP并保存到Excel----#爬取当日的代理IP并保存到Excel,目标网站xicidaili.co ...

  3. python代理池的构建3——爬取代理ip

    上篇博客地址:python代理池的构建2--代理ip是否可用的处理和检查 一.基础爬虫模块(Base_spider.py) #-*-coding:utf-8-*- ''' 目标: 实现可以指定不同UR ...

  4. python 批量爬取代理ip

    import urllib.request import re import time import random def getResponse(url): req = urllib.request ...

  5. 爬虫爬取代理IP池及代理IP的验证

    最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...

  6. 自动爬取代理IP例子

    import time import json import datetime import threading import requests from lxml import etree from ...

  7. 爬取代理IP

    现在爬虫好难做啊,有些网站直接封IP,本人小白一个,还没钱,只能找免费的代理IP,于是去爬了西刺免费代理,结果技术值太低,程序还没调试好, IP又被封了... IP又被封了... IP又被封了... ...

  8. 爬取代理IP,并判断是否可用。

    # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_all() import urllib2 from gevent.pool ...

  9. 原创:Python爬虫实战之爬取代理ip

    编程的快乐只有在运行成功的那一刻才知道QAQ 目标网站:https://www.kuaidaili.com/free/inha/  #若有侵权请联系我 因为上面的代理都是http的所以没写这个判断 代 ...

随机推荐

  1. 记账本微信小程序开发六

    记账本微信小程序开发六 我的界面 主界面

  2. 51Nod 1185 威佐夫游戏 V2

    有2堆石子.A B两个人轮流拿,A先拿.每次可以从一堆中取任意个或从2堆中取相同数量的石子,但不可不取.拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出2堆石子的数量, ...

  3. Linux 跟踪连接netfilter 调优

    Netfilter介绍 linux内核中的netfilter是一款强大的基于状态的防火墙,具有连接跟踪(conntrack)的实现.conntrack是netfilter的核心,许多增强的功能,例如, ...

  4. oracle insert、append、parallel、随后查询的redo与磁盘读写

    SQL> set autotrace traceonly statistics; SQL> insert into big_table_dir_test1 select * from bi ...

  5. Python实战:网络爬虫都能干什么?

    整站下载器:siteSucker https://blog.csdn.net/l_215851356/article/details/54377582 http://python.jobbole.co ...

  6. Ansible 的安装

    On Fedora: $ sudo dnf install ansible On RHEL and CentOS: $ sudo yum install ansible On Ubuntu: $ su ...

  7. 第十届蓝桥杯2019年C/C++ 大学B组省赛试题

    2019年第十届蓝桥杯大赛软件类省赛C/C++大学B组 试题 A:组队 本题总分:5分 [问题描述] 作为篮球队教练,你需要从以下名单中选出 1号位至 5号位各一名球员, 组成球队的首发阵容. 每位球 ...

  8. Mysql中 in or exists not exists not in区别 (网络整理)

    in 和or区别: 如果in和or所在列有索引或者主键的话,or和in没啥差别,执行计划和执行时间都几乎一样. 如果in和or所在列没有 索引的话,性能差别就很大了.在没有索引的情况下,随着in或者o ...

  9. CSS基础学习(二) 之 width min-width max-width属性

    width 1. 设置元素内容区(content area)的宽度. 2. 如果box-szing属性设置为border-box,width表示border area的宽度,如下图 min-width ...

  10. centos7 安装xinetd,telnet

    安装方式:yum [root@master ~]# yum list |grep telnettelnet-server.x86_64                    1:0.17-59.el7 ...