-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_meet.py
85 lines (60 loc) · 1.81 KB
/
first_meet.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
'''
1. Парсер однопоточный.
2. Замер времени
3. Multiprocessing Pool
4. Замер времени
5. Экспорт в csv
'''
import requests
from bs4 import BeautifulSoup
from datetime import datetime
from multiprocessing import Pool
import csv
import time
def get_html(url):
r = requests.get(url) # Response
return r.text # Возвращает HTML-код страницы(url)
def get_all_links(html):
counter = 0
soup = BeautifulSoup(html, 'lxml')
tags_div = soup.find('div').find_all('div', class_="cmc-table__column-name sc-1kxikfi-0 eTVhdN")
links = []
for td in tags_div:
a = td.find('a').get('href') #string
link = "https://coinmarketcap.com" + a
links.append(link)
return links
def get_page_data(html):
soup = BeautifulSoup(html, 'lxml')
try:
name = soup.find("h1").text.strip()
except:
name = ""
try:
price = soup.find("span", class_="cmc-details-panel-price__price").text.strip()
except:
price = ""
data = {'name': name, 'price': price}
return data
def write_csv(data):
with open('coinmarketcap.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow((data['name'],
data['price']))
print(data['name'], 'parsed')
def make_all(url):
html = get_html(url)
data = get_page_data(html)
write_csv(data)
# time.sleep(5)
def main():
start = time.time()
url = "https://coinmarketcap.com/all/views/all/"
all_links = get_all_links(get_html(url))
with Pool(40) as p:
p.map(make_all, all_links)
end = time.time()
total = end - start
print(str(total))
if __name__ == "__main__":
main()