-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
70 lines (50 loc) · 2.56 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
# -*- coding: utf-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import csv
"""Программа-парсер, которая берет данные с сайта и записывает их в таблицу формата CSV """
BASE_URL = 'https://www.weblancer.net/jobs/'
def get_html(url):
"""Данный метод берет запрос на страницу и читает данные"""
response = request.urlopen(url)
return response.read()
def get_page_count(html):
"""В этом методе берем все номера страниц"""
soup = BeautifulSoup(html, 'lxml')
pagination = soup.find('ul', class_= 'pagination')
number = pagination.find_all('a')[-1]
return int(((number.get('href'))[12:]))
def parse(html):
"""Здесь, собственно, парсим каждую страницу отдельно,
заносим информацию в словарь"""
soup = BeautifulSoup(html, 'lxml')
table = soup.find('div', class_= 'container-fluid cols_table show_visited')
projects = []
for row in table.find_all('div', class_='row'):
cols = row.find_all('div', class_='col-sm-7')
projects.append({
'title': cols[0].a.text,
'categories': [category.text for category in cols[0].div.find_all('a',class_='text-muted')],
'price': [price.text.strip() for price in row.find_all('div',class_='col-sm-2 amount title')][0],
'application': [app.text.strip() for app in row.find_all('div',class_='col-sm-3 text-right text-nowrap hidden-xs')][0]
})
return projects
def save(projects, path):
"""В этом методе записываем по категориям все данные,
в таком порядке, как они были записаны раннее"""
with open(path, 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(('Проект', 'Категории', 'Цена', 'Заявки'))
writer.writerows(
(project['title'], ', '.join(project['categories']), project['price'], project['application']) for project in projects
)
def main():
page_count = get_page_count(get_html(BASE_URL))
print('Всего найдено страниц %d ' % page_count)
projects = []
for page in range(1, 5):
print('Парсинг %d%%' % ( page / page_count * 100))
projects.extend(parse(get_html(BASE_URL + '?page=%d' % page)))
save(projects, 'projects.csv')
if __name__ == '__main__':
main()