I am trying to scrape this table: https://www.coingecko.com/en/coins/recently_added?page=1
Here is my code:
JavaScript
x
40
40
1
import requests
2
from bs4 import BeautifulSoup
3
import csv
4
5
root_url = "https://www.coingecko.com/en/coins/recently_added"
6
html = requests.get(root_url)
7
soup = BeautifulSoup(html.text, 'html.parser')
8
9
paging = soup.find("div",{"class":"row no-gutters tw-flex flex-column flex-lg-row tw-justify-end mt-2"}).find("ul",{"class":"pagination"}).find_all("a")
10
start_page = paging[1].text
11
last_page = paging[len(paging)-2].text
12
13
#
14
# outfile = open('gymlookup.csv','w', newline='')
15
# writer = csv.writer(outfile)
16
# writer.writerow(["Name", "Address", "Phone"])
17
18
19
pages = list(range(1,int(last_page)+1))
20
for page in pages:
21
url = 'https://www.coingecko.com/en/coins/recently_added?page=%s' %(page)
22
html = requests.get(url)
23
soup = BeautifulSoup(html.text, 'html.parser')
24
25
#print(soup.prettify())
26
print ('Processing page: %s' %(page))
27
28
coins = soup.findAll("div",{"class":"coingecko-table"})
29
for element in coins:
30
coin = element.find(class_='coin-name text-left tablesorter-header tablesorter-headerUnSorted')
31
price = element.find(class_='price text-right sorter-numeric tablesorter-header tablesorter-headerUnSorted')
32
print(coin,price)
33
# hr = element.find('change1h').text
34
# last_added = element.find('last_added').text
35
36
# writer.writerow([coin, price, hr,last_added])
37
#
38
# outfile.close()
39
print('Done')
40
The print(coin,price) fails to print anything. Not sure why, any help welcome :)
Advertisement
Answer
Just use pandas
to get the table data.
Here’s how:
JavaScript
1
8
1
import pandas as pd
2
import requests
3
4
url = "https://www.coingecko.com/en/coins/recently_added?page=1"
5
df = pd.read_html(requests.get(url).text, flavor="bs4")
6
df = pd.concat(df).drop(["Unnamed: 0", "Unnamed: 1"], axis=1)
7
df.to_csv("your_table.csv", index=False)
8
Output: