-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
132 lines (91 loc) · 3.54 KB
/
Parser.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys
if sys.version_info[0] < 3:
import urllib2
else:
from urllib.request import Request, urlopen
import requests
from fake_useragent import UserAgent
from bs4 import BeautifulSoup
import random
class Parser:
def return_page_content(self, url):
proxy_list=self.proxies_list()
print(proxy_list)
ua = UserAgent()
if sys.version_info[0] < 3:
indic = 1 #
while(indic == 1):
proxy_index = random.randint(0, len(proxy_list) - 1)
proxy = proxy_list[proxy_index]
req = urllib2.Request(url)
req.add_header('User-Agent', ua.random)
req.set_proxy(proxy['ip'] + ':' + proxy['port'], 'http')
try:
print("try")
page = urllib2.urlopen(req, timeout=4)
indic = 0
soup = BeautifulSoup(page, 'html.parser')
if(page.getcode() == "200"):
indic = 0
except Exception as e:
print("except")
del proxy_list[proxy_index]
print(len(proxy_list))
print(e)
pass
return soup
else:
indic = 1
while(indic == 1):
proxy_index = random.randint(0, len(proxy_list) - 1)
proxy = proxy_list[proxy_index]
# req = Request(url)
# req.add_header('User-Agent', ua.random)
# req.set_proxy(proxy['ip'] + ':' + proxy['port'], 'http')
try:
print("tryy")
headers = {'User-Agent': ua.random}
page = requests.get(url, headers=headers)
# page = urlopen(req, timeout=10).read().decode('utf8')
indic = 0
soup = BeautifulSoup(page.content.decode(), 'html.parser')
if(page.status_code == "200"):
indec=0
except Exception as e:
del proxy_list[proxy_index]
print(len(proxy_list))
print(e)
pass
return soup
return 1
def proxies_list(self):
if sys.version_info[0] < 3:
ua = UserAgent()
proxies_req = urllib2.Request('https://www.sslproxies.org/')
proxies_req.add_header('User-Agent', ua.random)
proxies_doc = urllib2.urlopen(proxies_req).read().decode('utf8')
soup = BeautifulSoup(proxies_doc, 'html.parser')
proxies_table = soup.find(id='proxylisttable')
proxies = []
for row in proxies_table.tbody.find_all('tr'):
proxies.append({'ip': row.find_all('td')[0].string,'port': row.find_all('td')[1].string})
return proxies
else:
ua = UserAgent()
proxies_req = Request('https://www.sslproxies.org/')
proxies_req.add_header('User-Agent', ua.random)
proxies_doc = urlopen(proxies_req).read().decode('utf8')
soup = BeautifulSoup(proxies_doc, 'html.parser')
proxies_table = soup.find(id='proxylisttable')
proxies = []
for row in proxies_table.tbody.find_all('tr'):
proxies.append({
'ip': row.find_all('td')[0].string,
'port': row.find_all('td')[1].string
})
return proxies
if __name__ == "__main__":
# execute only if run as a script
parse = Parser()
parse_content = parse.return_page_content("https://openclassrooms.com")
print(parse_content.find_all('a'))