python扫描proxy并且如何获取可用代理ip的示例分享


本文摘自php中文网,作者黄舟,侵删。

下面小编就为大家带来一篇python扫描proxy并获取可用代理ip的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

今天咱写一个挺实用的工具,就是扫描并获取可用的proxy

首先呢,我先百度找了一个网站:www.xicidaili.com 作为例子

这个网站里公布了许多的国内外可用的代理的ip和端口

我们还是按照老样子进行分析,就先把所有国内的proxy扫一遍吧

点开国内部分进行审查发现,国内proxy和目录为以下url:

www.xicidaili.com/nn/x

这个x差不多两千多页,那么看来又要线程处理了。。。

老样子,我们尝试是否能直接以最简单的requests.get()获取内容

返回503,那么我们加一个简单的headers

返回200,成咯

好了我们先进行网页内容分析并获取想要的内容

我们发现,包含ip信息的内容在<tr>标签内,于是我们就能很方便的用bs进行获取标签内容

但是我们随之又发现,ip、端口、协议的内容分别在提取的<tr>标签的第2,3,6三个<td>标签内

于是我们开始尝试编写,一下为编写思路:

  处理页面的时候,是先提取tr标签,再将tr标签中的td标签提取

  所以运用了两次bs操作,并且第二次使用bs操作时需要进行str处理

  因为我们获得tr之后,我们需要其中的2,3,6号的东西,

  但是当我们用一个for循环输出的i并不能进行组的操作

  所以我们干脆分别对每一个td的soup进行第二次操作之后直接提取2,3,6

  提取之后,直接加上.string提取内容即可


1

2

3

4

5

6

7

8

9

10

11

12

13

r = requests.get(url = url,headers = headers)

 soup = bs(r.content,"html.parser")

 data = soup.find_all(name = 'tr',attrs = {'class':re.compile('|[^odd]')})

 for i in data:

 

  soup = bs(str(i),'html.parser')

  data2 = soup.find_all(name = 'td')

  ip = str(data2[1].string)

  port = str(data2[2].string)

  types = str(data2[5].string).lower()

 

  proxy = {}

  proxy[types] = '%s:%s'%(ip,port)

这样,我们每次循环都能生成对应的proxy字典,以便我们接下来验证ip可用性所使用

字典这儿有个注意点,我们有一个将types变为小写的操作,因为在get方法中的proxies中写入的协议名称应为小写,而网页抓取的是大写的内容,所以进行了一个大小写转换

那么验证ip可用性的思路呢

很简单,我们使用get,加上我们的代理,请求网站:

http://1212.ip138.com/ic.asp

这是一个神奇的网站,能返回你的外网ip是什么


1

2

url = 'http://1212.ip138.com/ic.asp'

r = requests.get(url = url,proxies = proxy,timeout = 6)

这里我们需要加上timeout去除掉那些等待时间过长的代理,我设置为6秒

我们以一个ip进行尝试,并且分析返回的页面

返回的内容如下:


1

2

3

4

5

6

7

8

9

10

11

<html>

 

<head>

 

<meta xxxxxxxxxxxxxxxxxx>

 

<title> 您的IP地址 </title>

 

</head>

 

<body style="margin:0px"><center>您的IP是:[xxx.xxx.xxx.xxx] 来自:xxxxxxxx</center></body></html>

那么我们只需要提取出网页内[]的内容即可

如果我们的代理可用,就会返回代理的ip

(这里会出现返回的地址还是我们本机的外网ip的情况,虽然我也不是很清楚,但是我把这种情况排除,应该还是代理不可用)

那么我们就能进行一个判断,如果返回的ip和proxy字典中的ip相同,则认为这个ip是可用的代理,并将其写入文件

我们的思路就是这样,最后进行queue和threading线程的处理即可

上代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

#coding=utf-8

 

import requests

import re

from bs4 import BeautifulSoup as bs

import Queue

import threading

 

class proxyPick(threading.Thread):

 def __init__(self,queue):

  threading.Thread.__init__(self)

  self._queue = queue

 

 def run(self):

  while not self._queue.empty():

   url = self._queue.get()

 

   proxy_spider(url)

 

def proxy_spider(url):

 headers = {

   .......

  }

 

 r = requests.get(url = url,headers = headers)

 soup = bs(r.content,"html.parser")

 data = soup.find_all(name = 'tr',attrs = {'class':re.compile('|[^odd]')})

 

 for i in data:

 

  soup = bs(str(i),'html.parser')

  data2 = soup.find_all(name = 'td')

  ip = str(data2[1].string)

  port = str(data2[2].string)

  types = str(data2[5].string).lower()

 

 

  proxy = {}

  proxy[types] = '%s:%s'%(ip,port)

  try:

   proxy_check(proxy,ip)

  except Exception,e:

   print e

   pass

 

def proxy_check(proxy,ip):

 url = 'http://1212.ip138.com/ic.asp'

 r = requests.get(url = url,proxies = proxy,timeout = 6)

 

 f = open('E:/url/ip_proxy.txt','a+')

 

 soup = bs(r.text,'html.parser')

 data = soup.find_all(name = 'center')

 for i in data:

  a = re.findall(r'\[(.*?)\]',i.string)

  if a[0] == ip:

   #print proxy

   f.write('%s'%proxy+'\n')

   print 'write down'

    

 f.close()

 

#proxy_spider()

 

def main():

 queue = Queue.Queue()

 for i in range(1,2288):

  queue.put('http://www.xicidaili.com/nn/'+str(i))

 

 threads = []

 thread_count = 10

 

 for i in range(thread_count):

  spider = proxyPick(queue)

  threads.append(spider)

 

 for i in threads:

  i.start()

 

 for i in threads:

  i.join()

 

 print "It's down,sir!"

 

if __name__ == '__main__':

 main()

这样我们就能把网站上所提供的能用的代理ip全部写入文件ip_proxy.txt文件中了

以上就是python扫描proxy并且如何获取可用代理ip的示例分享的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python实现客户端和服务器端传输图片的代码

Python idle是什么

Python如何停止运行

Python接口如何返回json字符串

Python中如何安装pip

Python中表示字符串的几种方法介绍

Python list是否包含另一个list所有元素

字符串格式化 % vs format哪种更好

Python中关于列表list的整数操作与字符操作以及矩阵操作的实例分析

只学Python能找工作吗

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...