def \_\_init\_\_(self):
pass
def page\_manong(self):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
html=requests.get('https://proxy.coderbusy.com/classical/https-ready.aspx',verify=False,headers=headers)
# verify不验证安全证书(SSL),headers传进去将requests请求伪装成浏览器请求
if html.status_code == 200:
# 确保返回页面
Soup=BF(html.text,'lxml')
tbody=Soup.find('tbody')
tr_list=tbody.find_all('tr')
for tr in tr_list:
try:
IP_adress=tr.find('td').get_text().strip()
IP_port=tr.find('td',class_="port-box").get_text()
IP="http://"+IP_adress+":"+IP_port
# 用字符串加法构造IP
proxies={'http':IP}
try:
html=requests.get('http://www.baidu.com',proxies=proxies)
RO.put_head(IP)
if RO.list_len() > 30:
#这里定义如果存储的ip大于30个就跳出这个函数
return
print('valid IP')
except Exception:
print('invalid IP')
except Exception:
pass
else:
print('码农代理出错')
def page\_kuai(self):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
html=requests.get('https://www.kuaidaili.com/free/',headers=headers,verify=False)
if html.status_code == 200:
Soup=BF(html.text,'lxml')
tbody=Soup.find('tbody')
tr_list=tbody.find_all('tr')
for tr in tr_list:
try:
IP_adress=tr.find('td').get_text()
IP_port=tr.find('td',attrs={'data-title':"PORT"}).get_text()
IP="http://"+IP_adress+":"+IP_port
proxies={'http':IP}
try:
html = requests.get('http://www.baidu.com', proxies=proxies)
RO.put_head(IP)
if RO.list_len() > 30:
return
print('valid IP')
except Exception:
print('invalid IP')
except Exception:
pass
else:
print('快代理出错')
def page\_xici(self):
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
html=requests.get("http://www.xicidaili.com/",headers=headers,verify=False)
if html.status_code == 200:
htmltext=html.text
pattern=re.compile('td.\*?img.\*?</td>\s\*?<td>(.\*?)</td>\s\*?<td>(\d+)</td',re.S)
IP_zu=pattern.findall(htmltext)
for tr in IP_zu:
try:
IP='http://'+tr[0]+':'+tr[1]
try:
proxies = {'http': IP}
html = requests.get('http://www.baidu.com', proxies=proxies)
RO.put_head(IP)
if RO.list_len() > 30:
return
print('valid IP')
except Exception:
print('invalid IP')
except Exception:
pass
else:
print('西刺代理出错')
def page\_data5u(self):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'}
html = requests.get("http://www.data5u.com/free/gnpt/index.shtml", headers=headers, verify=False)
if html.status_code == 200:
Soup=BF(html.text,'lxml')
li=Soup.find('li',style="text-align:center;")
ul=li.find_all('ul',class_="l2")
for tr in ul:
try:
IP_adress=tr.find('span').get_text()
IP_port=tr.find('span',style="width: 100px;").get_text()
IP="http://"+IP_adress+":"+IP_port
try:
proxies = {'http': IP}
html = requests.get('http://www.baidu.com', proxies=proxies)
RO.put_head(IP)
if RO.list_len() > 30:
return
print('valid IP')
except Exception:
print('invalid IP')
except Exception:
pass
class run_parser:
这里用于在其他的文件中调用这个文件的函数和方法
# 用于调用上面的进程
def Run\_Parser(self):
x = IP_page_parser()
process_list = []
# 这里我开起了一个多线程,同时对多个页面进行抓取和测试
t1 = threading.Thread(target=x.page_manong, args=())
process_list.append(t1)
t2 = threading.Thread(target=x.page_kuai, args=())
process_list.append(t2)
t3 = threading.Thread(target=x.page_xici, args=())
process_list.append(t3)
t4 = threading.Thread(target=x.page_data5u, args=())
process_list.append(t4)
for i in process_list:
i.start()
for i in process_list:
i.join()
RP=run_parser() # 这个用于导出上面类的实例。
if name==‘__main__’:
x=IP_page_parser()
process_list=[]
t1=threading.Thread(target=x.page_manong,args=())
process_list.append(t1)
t2=threading.Thread(target=x.page_kuai,args=())
process_list.append(t2)
t3=threading.Thread(target=x.page_xici,args=())
process_list.append(t3)
t4=threading.Thread(target=x.page_data5u,args=())
process_list.append(t4)
for i in process_list:
i.start()
for i in process_list:
i.join()
上面一段是最长的代码,一百五十行,而且大部分是重复的。你可以看完的
**上面一步几乎已经完成了大部分的工作,接下来我们要测定已经存储的ip有没有用,注意,上面一步和这一步一定是有且只有一个在执行,做法我们接下来会给出**
这是代码
mport requests
from ProxyFile.IP_store import Redis_Operation as R_O
注意对IP_store.py文件的引用
from ProxyFile.IP_store import *
class List_Ip_test:
def get\_and\_test(self):
# 从列表的尾部取出一个ip
ip=str(RO.get_tail(),encoding='utf-8')
# redis导出的数据都是bytes类型的,所以我们必须将其str化,必须家enconding参数,详见《python学习手册》高级话题部分
proxies = {'http': ip}
# 测试ip有没有用
html = requests.get('http://www.baidu.com', proxies=proxies)
if html.status_code == 200:
RO.put_head(ip)
print('valid IP')
else:
print('丢弃无用的ip')
LIT=List_Ip_test() # 创建一个实例,用于其他文件的引用
**好了,这次我们真的完成了几乎距大部分的工作了,接下来还有一个调用的文件**
我只把代码贴出来把
首先,文件api.py,这是一个接口文件,
coding:utf-8
用于做接口,使其他的程序能够获得这个程序的开发出来的有用的IP
from flask import Flask
from ProxyFile.IP_store import *
all = [‘app’]
app = Flask(name)
@app.route(‘/’)
def get_proxy():
return RO.get_head()
app.run() # 当你运行这段代码时,在浏览器中输入localhost:5000,就会出现ip
接下来是scheduler.py文件,用与调用整个程序
coding:utf-8
用于对redis数据库的一些调用,检查IP和添加IP
from ProxyFile.page_parser import *
from ProxyFile.IP_store import Redis_Operation as R_O
from ProxyFile.IP_store import *
from ProxyFile.list_IP_test import *
import time
class Add_and_Check:
def add_and_check(self):
# 当ip池中小于十个ip那么就在网页上爬取,否则就不断测试现在的ip是不是还有用
while True:
# 程序是一直在运行的,运行着Run_Parser()函数或者是get_and_text()函数
if RO.list_len() < 30:
RP.Run_Parser()
else:
LIT.get_and_test()
time.sleep(30) # 当数据库中有了三十个ip时可以休息一下在从新运行