#coding:utf-8

from bs4 import BeautifulSoup
import time
import threading
import random
import telnetlib,requests #设置全局超时时间为3s,也就是说,如果一个请求3s内还没有响应,就结束访问,并返回timeout(超时)
import socket
socket.setdefaulttimeout(3) headers = {
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
} def get_ip():
#获取代理IP,返回列表
httpResult=[]
httpsResult=[]
try:
for page in range(1,10):
IPurl = 'http://www.xicidaili.com/nn/%s' %page
rIP=requests.get(IPurl,headers=headers)
IPContent=rIP.text
#print (IPContent)
soupIP = BeautifulSoup(IPContent,'html.parser')#lxml
trs = soupIP.find_all('tr')
for tr in trs[1:]:
tds = tr.find_all('td')
ip = tds[1].text.strip()
port = tds[2].text.strip()
protocol = tds[5].text.strip()
if protocol == 'HTTP':
httpResult.append( 'http://' + ip + ':' + port)
elif protocol =='HTTPS':
httpsResult.append( 'https://' + ip + ':' + port)
except Exception as inst:
print (inst)
return httpResult,httpsResult #验证ip地址的可用性,使用requests模块,验证地址用相应要爬取的网页 http
def cip(x,y):
f = open("E:\ip_http.txt","a")
f.truncate()
try:
print (x+y)
requests.get('http://ip.chinaz.com/getip.aspx',proxies={'http':x+":"+y},timeout=3)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n')
#验证ip地址的可用性,使用requests模块,验证地址用相应要爬取的网页。https
def csip(x,y):
f = open("E:\ip_https.txt","a")
f.truncate()
try:
print (x+y)
requests.get('https://www.lagou.com/',proxies={'https':x+":"+y},timeout=3)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n') def main():
httpResult,httpsResult = get_ip()
print(len(httpResult), len(httpsResult))
threads = []
open("E:\ip_http.txt","a").truncate()
for i in httpResult:
a = str(i.split(":")[-2][2:].strip())
b = str(i.split(":")[-1].strip())
t = threading.Thread(target=cip,args=(a,b,))
threads.append(t) for i in range(len(httpResult)):
threads[i].start()
for i in range(len(httpResult)):
threads[i].join() threads1 = []
open("E:\ip_https.txt","a").truncate()
for i in httpsResult:
a = str(i.split(":")[-2][2:].strip())
b = str(i.split(":")[-1].strip())
t = threading.Thread(target=csip,args=(a,b,))
threads1.append(t) for i in range(len(httpsResult)):
threads1[i].start()
for i in range(len(httpsResult)):
threads1[i].join() if __name__ == '__main__':
main()

python 爬取可用的更多相关文章

  1. 利用Python爬取可用的代理IP

    前言 就以最近发现的一个免费代理IP网站为例:http://www.xicidaili.com/nn/.在使用的时候发现很多IP都用不了. 所以用Python写了个脚本,该脚本可以把能用的代理IP检测 ...

  2. Python:爬取乌云厂商列表,使用BeautifulSoup解析

    在SSS论坛看到有人写的Python爬取乌云厂商,想练一下手,就照着重新写了一遍 原帖:http://bbs.sssie.com/thread-965-1-1.html #coding:utf- im ...

  3. 萌新学习Python爬取B站弹幕+R语言分词demo说明

    代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...

  4. 利用python爬取58同城简历数据

    利用python爬取58同城简历数据 利用python爬取58同城简历数据 最近接到一个工作,需要获取58同城上面的简历信息(http://gz.58.com/qzyewu/).最开始想到是用pyth ...

  5. 用Python爬取网易云音乐热评

    用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...

  6. Python 爬取所有51VOA网站的Learn a words文本及mp3音频

    Python 爬取所有51VOA网站的Learn a words文本及mp3音频 #!/usr/bin/env python # -*- coding: utf-8 -*- #Python 爬取所有5 ...

  7. python爬取网站数据

    开学前接了一个任务,内容是从网上爬取特定属性的数据.正好之前学了python,练练手. 编码问题 因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了. 问题要从文字的编码讲 ...

  8. python爬取某个网页的图片-如百度贴吧

    python爬取某个网页的图片-如百度贴吧 作者:vpoet mail:vpoet_sir@163.com 注:随意copy,不用告诉我 #coding:utf-8 import urllib imp ...

  9. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

随机推荐

  1. iptables配置管理

    iptables规则 内核netfilter的规则: iptables的规则: 基本操作 实践:往iptables中插入一条规则:禁止基于端口号为22的所有客户端访问(等于是禁止基于SSH的客户端连接 ...

  2. 分享一些 Windows 平台上的神器

    下面分享一些 Windows 平台上日常开发使用的软件,有些软件我自认为是神器,可以大大提高效率. 编辑器类软件 IntelliJ IDEA IntelliJ IDEA 内部集成 Java 开发环境, ...

  3. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  4. LeetCode 176. 第二高的薪水(MySQL版)

    0.前言 最近刷LeetCode 刷数据库题目 由于数据库课上的是SQL,而MySQL有许多自己的函数的,怕把刚学会的函数忘记 特在此记录! 1.题目 编写一个 SQL 查询,获取 Employee ...

  5. Hacking /dev/random: Pipe

    1) 下载镜像,虚拟机启动 https://download.vulnhub.com/devrandom/pipe.ova 2) nmap 扫描探测 Nmap 扫描开路: ╰─ nmap -Pn -s ...

  6. mininet安装过程记录

    参考文档: http://www.brianlinkletter.com/set-up-mininet/ https://github.com/mininet/mininet/wiki/FAQ#x11 ...

  7. OpenCL中的half与float的转换

    在kernel中使用half类型可以在牺牲一定精度的代价下来提升运算速度. 在kernel中, 可以比较方便的对half数据进行计算, 但在host上的, 对half的使用就没那么方便了. 查看cl_ ...

  8. 常用Shell脚本命令(备忘)

    此处纪录一些个人常用的Shell命令,留作复用 Linux 必备软件 Tmux 终端复用神器 zsh 无比强大Shell运行环境 oh my zsh 搭配zsh食用 uGet Linux下载工具 Do ...

  9. 质量:“PM,你怎么可以放弃我?!”

    最近遇到一件有趣的事儿,一位文玩老友为了攒一串多宝,新得几颗高品松石,开心不已与我们分享.闲扯中,他说出了攒多宝的要求: 直径偏差2mm以内 每种品类的珠子都要是高品 品类只能是南红(满色满肉).蜜蜡 ...

  10. 一天一个Linux命令--dhclient

    dhclient -r #用于释放ip地址2 dhclient #获取IP地址 主要针对只有命令行的Linux机器,临时改变了网络环境,事先手动设置的ip地址 看一下自带的解释 root@ubuntu ...