-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.py
52 lines (36 loc) · 1.14 KB
/
3.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
import requests # парсинг криптовалют
from bs4 import BeautifulSoup
import csv
def get_html(url):
r = requests.get(url)
return r.text
def clean_data(price):
price = price.replace('.', '')
price = price.replace(',', '.')
return price
def write_csv(data):
with open('coin.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow([data['name'],
data['symbol'],
data['url'],
data['price']])
def get_data(html):
soup = BeautifulSoup(html, 'lxml')
trs = soup.find('table').find('tbody').find_all('tr')
for tr in trs:
tds = tr.find_all('td')
name = tds[2].find('a').text
symbol = tds[3].text.strip()
url = 'https://ru.investing.com' + tds[2].find('a').get('href')
price = clean_data(tds[4].text)
data = {'name': name,
'symbol': symbol,
'url': url,
'price': price}
write_csv(data)
def main():
url = 'https://ru.investing.com/crypto/'
get_data(get_html(url))
if __name__ == '__main__':
main()