-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebScraper.py
More file actions
85 lines (51 loc) · 1.31 KB
/
WebScraper.py
File metadata and controls
85 lines (51 loc) · 1.31 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
83
84
85
import requests
from bs4 import BeautifulSoup
import csv
from datetime import datetime
from multiprocessing import Pool
def get_html(url):
r = requests.get(url)
return r.text
def get_all_links(html):
soup = BeautifulSoup(html, 'lxml')
tds = soup.find('table', id = 'currencies-all').find_all('td', class_='currency-name')
links = []
for td in tds:
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', class_='text-large').text.strip()
except:
name = ''
try:
price = soup.find('span', id='quote_price').text.strip()
except:
price = ''
data = {'name': name,
'price': price}
return data
def write_data(data):
with open('coinmarket.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow( (data['name'],
data['price']) )
print(data['name'], data['price'])
def make_all(url):
html = get_html(url)
data = get_page_data(html)
write_data(data)
def main():
start = datetime.now()
url = 'https://coinmarketcap.com/all/views/all/'
all_links = get_all_links( get_html(url) )
with Pool(20) as p:
p.map(make_all, all_links)
end = datetime.now()
total = end = start
print(str(total))
if __name__ == '__main__':
main()