我试图为这个站点抓取html表,但无法获取chhange(24h)列。
from requests import get
from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd
import matplotlib.pyplot as plt
content = urlopen("https://coinmarketcap.com/")
soup = BeautifulSoup(content, 'html.parser')
rows = soup.find_all('tr')
for row in rows:
row.find('td', {'data-timespan': '24h'}).text)无法获取用于更改的标记(24小时),而我们尝试调试行时,它包含该标记
发布于 2018-08-14 08:37:22
源页面显示,它们关闭了一个额外的锚标记,就在您需要的元素附近。所以,bsoup无法得到它。很可能,这是故意的,是为了引入一层复杂性,让人们放弃它。P或可能是无意的。

查看图像中标记文本的第一行和最后一行,即两个标记,这给bSoup的DOM解析器造成了混乱。
因此,解决方案是直接找到这些元素,而不是遍历每个元素的子元素,因为这就是罪魁祸首结束标记存在的地方。
from requests import get
from urllib.request import urlopen
from bs4 import BeautifulSoup
content = urlopen("https://coinmarketcap.com/")
soup = BeautifulSoup(content, 'html.parser')
rows = soup.find_all('td', {'data-timespan': '24h'})
for row in rows:
print(row)这给了你想要的。另一种方法是通过循环中的RegExp模式匹配器从"row“(引用代码)找到所需的元素。
发布于 2018-08-14 08:23:47
嘿,你可以用我做的包裹.scrape
简单
from easy_scrape.scrape_table import scrape_table
scrape_obj =scrape_table(#give your path to the chrome driver)
data = scrape_obj.table(url = 'https://coinmarketcap.com/' , class_name='dataTable')注意:别忘了
pip安装selenium & pip安装easy_scrape
注:此外,请根据需要编辑结果。
PS :请检查他们的robots.txt
https://stackoverflow.com/questions/51836352
复制相似问题