python - 无法使用 beautifulsoup 在网站上刮桌子
问题描述
我正在尝试刮掉这张桌子:https ://www.coingecko.com/en/coins/recently_added?page=1
这是我的代码:
import requests
from bs4 import BeautifulSoup
import csv
root_url = "https://www.coingecko.com/en/coins/recently_added"
html = requests.get(root_url)
soup = BeautifulSoup(html.text, 'html.parser')
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")
start_page = paging[1].text
last_page = paging[len(paging)-2].text
#
# outfile = open('gymlookup.csv','w', newline='')
# writer = csv.writer(outfile)
# writer.writerow(["Name", "Address", "Phone"])
pages = list(range(1,int(last_page)+1))
for page in pages:
url = 'https://www.coingecko.com/en/coins/recently_added?page=%s' %(page)
html = requests.get(url)
soup = BeautifulSoup(html.text, 'html.parser')
#print(soup.prettify())
print ('Processing page: %s' %(page))
coins = soup.findAll("div",{"class":"coingecko-table"})
for element in coins:
coin = element.find(class_='coin-name text-left tablesorter-header tablesorter-headerUnSorted')
price = element.find(class_='price text-right sorter-numeric tablesorter-header tablesorter-headerUnSorted')
print(coin,price)
# hr = element.find('change1h').text
# last_added = element.find('last_added').text
# writer.writerow([coin, price, hr,last_added])
#
# outfile.close()
print('Done')
print(coin,price) 无法打印任何内容。不知道为什么,欢迎任何帮助:)
解决方案
推荐阅读
- python - 如何在对象初始化时调用具有特定命名模式的所有方法
- lilypond - 如何创建一个函数来自动创建标题?
- excel - 仅当单元格等于工作表/选项卡名称时,如何复制和粘贴整行?
- apache-kafka - 是否可以减少 min.insync.replicas?
- outlook - 撰写邮件时如何在 Outlook Web App 中显示插件?
- python - Slack API 对话。回复 internal_error
- java - 我如何通过使用字符串来使用可选类
- java - 使用 Scanner 查找未排序数组中的第 N 个最小数字
- python-3.x - 我的 API 返回空白 json,而不是所需的嵌套模式
- c++ - 在我的主应用程序中加载一个 dll 怎么会导致 100 个 CPU 负载?