1. 获取西刺代理,enjoy!
2. 我的打印机啊, 可以的,可以的。
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# -*- coding: utf-8 -*-
# info: fanchuang 2018/6/3 20:08
# 目的: 写一个获取西刺代理的小文件,用面向对象的思想
# 使用此文件: 1. d = DaiLi(); 2. d.enjoy()
import random
import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
class DaiLi:
def __init__(self):
self.door = 'http://www.xicidaili.com/nn/1'
self.headers = {'User-Agent': UserAgent().random}
self.s = requests.Session()
self.proxies = []
def getProxies(self):
resp = self.s.get(self.door, headers=self.headers)
if resp.status_code == 200:
soup = BeautifulSoup(resp.text, 'lxml')
tar = soup.find_all('tr', class_='odd')
for t in tar:
kids = t.find_all('td')
# 判断并过滤类型,个人认为HTTPS的好用一些。
if kids[5].text == "HTTPS":
# 判断并过滤存活时间
if "天" in kids[8].text:
# 拿到ip地址和端口号
daili = ":".join([kids[1].text, kids[2].text])
# print("ip + port:", daili)
self.proxies.append(daili)
else:
print('sorry,nothing: ' + str(resp.status_code))
return self.proxies
def enjoy(self):
return random.choice(self.getProxies())
if __name__ == '__main__':
d = DaiLi()
# d.enjoy()
print(d.enjoy())