【问题标题】:unable to web scrape all the data not fetching all <td> values无法通过网络抓取所有数据,无法获取所有 <td> 值
【发布时间】:2018-08-14 17:28:51
【问题描述】:

我正在尝试抓取此站点的 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)

在我们尝试调试 rows 时无法获取更改的标签(24 小时),它包含该标签

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    源页面显示,它们有一个额外的锚标记关闭,就在您需要的元素附近。所以,bsoup 是拿不到的。可能是有意引入一层复杂性,以便人们将其剔除。 :P 或者可能是无意的。

    看到图片中第一行和最后一行标记的文字,两个标签,给bSoup的DOM解析器造成混乱。

    所以解决方案是,直接找到这些元素,而不是遍历 each 的子元素,因为那是罪魁祸首结束标记所在的位置。

    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 模式匹配器在循环内从“行”(参考您的代码)中找到所需的元素。

    【讨论】:

    • rows = soup.find_all('tr') 这是查找所有 tr 标签的 ryt 吗?
    • 是的。但是当您遍历每个“tr”元素时,它的 innerText 被视为 DOM 树。由于额外的 标记,此树无效,这会导致 DOM Parser 无法找到所需的元素。在想法的情况下,它应该工作。但是,不在这里!
    • 如果对您有帮助,请慷慨地给评论评分。 :)
    【解决方案2】:

    你可以使用我制作的包...https://github.com/sarthaknegi/easy_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 安装硒 & pip install easy_scrape

    注意:另外,请根据需要编辑结果

    PS : 请查看他们的 robots.txt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      相关资源
      最近更新 更多