PyThon 版本 3.5
编辑器 Sublime Text
因为需要代理IP 在网上手动获取太过于繁琐,而且 代理IP 有时候还不可以用,就想利用脚本抓取一些,然后通过花刺代理工具验证,找到可用的IP代理。
BeautifulSoup 引用出错 原因 在3.5版本中 名称更改为 bs4 引用需要注意
urllib2 这个模块期初感觉请求慢,于是替换成了 httplib 结果速度差不多,
需要的时候运行一下脚本就可以,代码如下:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import urllib2
# from bs4 import BeautifulSoup 新版本在名称被更改引用请注意
import bs4
# import re
# import httplib
class GetRes:
def rest(self, c):
req = urllib2.Request("http://www.kuaidaili.com/free/inha/" + c)
con = urllib2.urlopen(req)
doc = con.read()
con.close()
# conn = httplib.HTTPConnection("www.kuaidaili.com")
# conn.request("GET","/proxylist/1")
# res = conn.getresponse()
# doc = res.read()
# conn.close()
soup = bs4.BeautifulSoup(doc, 'html.parser')
paper_name = soup.html.body.find_all('tr')
# print(paper_name)
for obj in paper_name:
par = obj.find_all('td')
# (attrs={u'data-title':u'IP'})
if len(par) >= 2:
re = par[0].string + ":" + par[1].string + "\n"
file = open('proxyip.txt', 'a+')
file = file.write(str(re))
# print(file)
# file.close()
res = GetRes()
count = 0
while (count < 100):
count = count + 1
print(count)
res.rest(str(count))
欢迎关注哟,每天更新;