【问题标题】:Scrapy: unable to locate table or scrape data in tableScrapy:无法定位表格或刮取表格中的数据
【发布时间】:2021-03-02 17:45:11
【问题描述】:

对于一个小组项目,我正在尝试在 https://www.basketball-reference.com/players/a/allenra02.html 中抓取 Salaries 表。

我尝试过多种 CSS 和 Xpath 选择器,例如

#all_salaries > tbody > tr:nth-child(1)
#all_salaries > tbody
#all_salaries > tbody > tr:nth-child(1) > td.right
#all_salaries
//*[@id="all_salaries"]/tbody/tr[1]/td[3]
//*[@id="all_salaries"]/tbody
//*[@id="all_salaries"]

代码如下:

def start_requests(self):
    start_urls = ['https://www.basketball-reference.com/players/a/allenra02.html']

    for url in start_urls:
        yield scrapy.Request(url=url, callback=self.parse_season)

def parse_player(self, response): 
 response.css('#all_salaries > tbody)

我尝试将其打印出来,但它一直返回一个空列表。 其他表看起来都不错,除了这个。

编辑:

我的最终解决方案看起来像

regex = re.compile(r'<!--(.*)-->', re.DOTALL)
    salaries = response.xpath('//*[@id="all_all_salaries"]/comment()').get()
    
    if salaries:
        salaries = response.xpath('//*[@id="all_all_salaries"]/comment()').re(regex)[0]
        salaries_sel = scrapy.Selector(text=salaries, type="html")
        all_salaries = salaries_sel.css('#all_salaries > tbody > tr').extract()

【问题讨论】:

  • 嘿@JWiryo 你还有解决方案吗?看了下面给出的答案,它显示了如何获得评论,但我还没有弄清楚如何获得任何内容......
  • 是的!让我编辑我的问题
  • 感谢@JWiryo,由于您的编辑,我现在能够获得所需的内容

标签: python web-scraping scrapy


【解决方案1】:

您可以使用 BeautifulSoup 提取 cmets,然后使用 pandas 解析表格。我选择只拉出salary table,但是你可以通过这种方式获取cmets中的所有表。

import requests
from bs4 import BeautifulSoup, Comment
import pandas as pd

url = "https://www.basketball-reference.com/players/a/allenra02.html"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

comments = soup.find_all(string=lambda text: isinstance(text, Comment))
tables = []
for each in comments:
    if 'table' in str(each):
        try:
            tables.append(pd.read_html(str(each), attrs = {'id': 'all_salaries'})[0])
            break
        except:
            continue
print(tables[0].to_string())

输出:

     Season                 Team   Lg        Salary
0   1996-97      Milwaukee Bucks  NBA    $1,785,000
1   1997-98      Milwaukee Bucks  NBA    $2,052,360
2   1998-99      Milwaukee Bucks  NBA    $2,320,000
3   1999-00      Milwaukee Bucks  NBA    $9,000,000
4   2000-01      Milwaukee Bucks  NBA   $10,130,000
5   2001-02      Milwaukee Bucks  NBA   $11,250,000
6   2002-03      Milwaukee Bucks  NBA   $12,375,000
7   2003-04  Seattle SuperSonics  NBA   $13,500,000
8   2004-05  Seattle SuperSonics  NBA   $14,625,000
9   2005-06  Seattle SuperSonics  NBA   $13,223,140
10  2006-07  Seattle SuperSonics  NBA   $14,611,570
11  2007-08       Boston Celtics  NBA   $16,000,000
12  2008-09       Boston Celtics  NBA   $18,388,430
13  2009-10       Boston Celtics  NBA   $18,776,860
14  2010-11       Boston Celtics  NBA   $10,000,000
15  2011-12       Boston Celtics  NBA   $10,000,000
16  2012-13           Miami Heat  NBA    $3,090,000
17  2013-14           Miami Heat  NBA    $3,229,050
18   Career  (may be incomplete)  NaN  $184,356,410

【讨论】:

    【解决方案2】:

    这是因为该表实际上在原始源代码中被注释掉,后来通过javascript添加。看这里如何获取评论内容:Scrapy: Extract commented (hidden) content

    【讨论】:

    • 啊,我明白了!让我检查一下!谢谢
    • 解决了!非常感谢!
    猜你喜欢
    • 2016-08-08
    • 2020-06-10
    • 2021-06-24
    • 2014-02-09
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多