-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
82 lines (66 loc) · 1.98 KB
/
Copy pathparser.py
File metadata and controls
82 lines (66 loc) · 1.98 KB
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
from urllib.request import urlopen
from urllib.parse import urlparse
from bs4 import BeautifulSoup
def data_url(link, f):
url = urlopen(link)
data_parse = url.read()
data_parse = data_parse.decode('windows-1251', 'ignore')
url.close()
soup = BeautifulSoup(data_parse, 'html.parser')
table_string = soup.find('table', id='tab_main1')
massive = list(table_string.findAll('tr'))
len_massive = len(massive)
i = int(2)
while i < len_massive:
td = list()
td.append(massive[i].select_one('td:nth-of-type(1)'))
td.append(massive[i].select_one('td:nth-of-type(2)'))
td.append(massive[i].select_one('td:nth-of-type(4)'))
td.append(massive[i].select_one('td:nth-of-type(8)'))
data_list = ''
data_list += str('<tr>')
for item in td:
data_list += str(item)
data_list += str('</tr>')
success = f.write(data_list)
i = i + 1
print('Has been written ' + str(len_massive) + 'string')
def parse_count_page(count_page):
massive = list(count_page.findAll('a'))
len_massive = len(massive)
if len_massive > 0:
f = open(file_name, 'w', encoding='utf-8')
f.write(str("""<table>
<thead>
<tr>
<td>Name</td>
<td>R1</td>
<td>Long</td>
<td>Price</td>
</tr>
</thead>
<tbody>"""))
for page in massive:
link = host_name + page.get('href')
data_url(link, f)
f.write(str('</tbody></table>'))
f.close()
else:
print('No goods for this address')
return False
def check_url(url):
open_url = urlopen(url)
data_parse = open_url.read()
data_parse = data_parse.decode('windows-1251', 'ignore')
open_url.close()
soup = BeautifulSoup(data_parse, 'html.parser')
count_page = soup.find('div', id='products_nav_list')
if not count_page:
print('Such address doesn`t exist')
return False
parse_count_page(count_page)
url = ''
host_name_parse = urlparse(url)
host_name = host_name_parse.scheme + str('://') + host_name_parse.netloc
file_name = 'main.xls'
check_url(url)