AttributeError:'功能'对象没有属性' find_all'美丽的汤

时间:2017-05-03 14:11:14

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

在浏览了大约十几个相同品种的问题并咨询同事后,我确定需要一些专家见解

with open("c:\source\list.csv") as f:
  for row in csv.reader(f):
    for url in row:
      r = requests.get(url)
      soup = BeautifulSoup(r.content, 'lxml')
      tables = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"}).append
      for rows in table.find_all('tr', {'releasetype': 'Current_Releases'}):
          item = [].append
          for val in row.find_all('td'):
            item.append(val.text.encode('utf8').strip())
          rows.append(item)
      headers = [header.text for header in tables.find_all('th')].append
      rows = [].append
      print (headers)

所以我在这里有一个:csv文件,里面有30个URL。我首先将它们转储到Soup中以获取其所有内容,然后将特定的HTML元素(表)绑定到tables变量。在此之后,我试图从这些表中提取特定的行和标题。

根据我脑中的逻辑思维,它应该有效,但相反,我得到了这个:

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
AttributeError: 'function' object has no attribute 'find_all'

第7行是

for rows in table.find_all('tr', {'releasetype': 'Current_Releases'}):

我们在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

您对Python语法有一些奇怪的误解。您的代码中有四次引用<something>.append;我不确定你认为这是什么,但append是一种方法,不仅必须使用()调用它,而且还需要一个参数:你要追加的东西。 / p>

所以,例如,这一行:

 item = [].append

毫无意义;你期待item是什么?你有什么希望追加?当然,你的意思是item = []

在特定情况下,错误是因为前一行末尾的多余append

tables = soup.find('table', attrs={"class": "hpui-standardHrGrid-table"}).append

再次,只需删除append