繁体   English   中英

AttributeError:“函数”对象没有属性“ find_all”美丽汤

[英]AttributeError: 'function' object has no attribute 'find_all' Beautiful Soup

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

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)

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

根据我大脑中的逻辑思维,它应该起作用,但是我得到的是:

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'}):

我们在这里想念什么?

您对Python语法有一些奇怪的误解。 您在代码中四次引用<something>.append ; 我不确定您的想法,但是append是一个方法,不仅必须使用()来调用它,而且还需要一个参数:您要附加的东西。

因此,例如,这一行:

 item = [].append

完全没有道理; 你有什么期待item是? 您希望追加什么? 当然,您只是说item = []

在特定情况下,该错误是由于前一行末尾的多余append

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

同样,只需删除append

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM