【问题标题】:Type Error: Result Set Is Not Callable - BeautifulSoup类型错误:结果集不可调用 - BeautifulSoup
【发布时间】:2019-05-12 01:41:16
【问题描述】:

我遇到了网络抓取问题。我正在尝试学习如何做到这一点,但我似乎无法超越一些基础知识。我收到一个错误,“TypeError: 'ResultSet' object is not callable”是我收到的错误。

我尝试了许多不同的方法。我最初试图使用“find”而不是“find_all”函数,但是我遇到了一个问题,即 beautifulsoup 引入了一个非类型。我无法创建一个可以克服该异常的 if 循环,因此我尝试使用“find_all”。

page = requests.get('https://topworkplaces.com/publication/ocregister/')

soup = BeautifulSoup(page.text,'html.parser')all_company_list = 
soup.find_all(class_='sortable-table')
#all_company_list = soup.find(class_='sortable-table')


company_name_list_items = all_company_list('td')

for company_name in company_name_list_items:
    #print(company_name.prettify())
    companies = company_name.content[0]

我希望这能够以干净的方式将加利福尼亚橙县所有在此列表中的公司纳入其中。如您所见,我已经完成了将它们拉入,但我希望列表是干净的。

【问题讨论】:

  • all_company_list('td') 是你的问题——这就像去[1,2,3,4]("td")。您不能像调用可调用函数一样调用列表。
  • 好吧,我不明白你的意思。如果您查看代码,您是否看到 print /prettify 语句被注释掉的位置?当我在那里运行它时它工作正常。

标签: html python-3.x web-scraping beautifulsoup


【解决方案1】:

你的想法是对的。我认为不是立即找到所有<td> 标签(它将为每一行(140 行)和行中的每一列(4 列)返回一个 <td>),如果你只想要公司名称,它可能更容易找到所有行(<tr> 标签),然后通过在每一行中迭代 <td>s 来附加您想要的任意多列。 这将获得第一列,公司名称:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://topworkplaces.com/publication/ocregister/')

soup = BeautifulSoup(page.text,'html.parser')
all_company_list = soup.find_all('tr')

company_list = [c.find('td').text for c in all_company_list[1::]]

现在company_list 包含所有 140 个公司名称:

 >>> print(len(company_list))

['Advanced Behavioral Health', 'Advanced Management Company & R³ Construction Services, Inc.',
...
, 'Wes-Tec, Inc', 'Western Resources Title Company', 'Wunderman', 'Ytel, Inc.', 'Zillow Group']

c.find('td') 更改为 c.find_all('td') 并迭代该列表以获取每个公司的所有列。

【讨论】:

    【解决方案2】:

    熊猫:

    Pandas 在这里通常很有用。该页面使用多种类型,包括公司规模、排名。我显示排名排序。

    import pandas as pd
    
    table = pd.read_html('https://topworkplaces.com/publication/ocregister/')[0]
    table.columns = table.iloc[0]
    table = table[1:]
    table.Rank = pd.to_numeric(table.Rank)
    rank_sort_table = table.sort_values(by='Rank', axis=0, ascending = True)
    rank_sort_table.reset_index(inplace=True, drop=True)
    rank_sort_table.columns.names = ['Index']
    print(rank_sort_table)
    

    根据您的排序,按顺序排列的公司:

    print(rank_sort_table.Company)
    

    请求:

    顺便说一句,您可以使用 nth-of-type 仅选择第一列(公司名称)并使用 id 而不是类名来更快地识别表

    import requests
    from bs4 import BeautifulSoup as bs
    
    r = requests.get('https://topworkplaces.com/publication/ocregister/')
    soup = bs(r.content, 'lxml')
    names = [item.text for item in soup.select('#twpRegionalList td:nth-of-type(1)')]
    print(names)
    

    请注意,默认排序是按名称列的字母而不是排名。


    参考:

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-07
      • 2018-07-16
      • 2020-11-20
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2017-03-08
      相关资源
      最近更新 更多