简体   繁体   中英

AttributeError: 'function' object has no attribute 'find_all' Beautiful Soup

After cruising through about a dozen questions of the same variety, and consulting a coworker, I have determined I need some expert insight

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)

So what I have here is : a csv file that has 30 URLs in it. I first dump them into Soup to get all of its contents, then bind the specific HTML element (the tables) to the tables variable. After this, i am trying to pull specific rows and headers from those tables.

According to logical thinking in my brain, it should work, but instead, i get this:

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

Line 7 is

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

What are we missing here?

You have some strange misconceptions about Python syntax. Four times in your code you refer to <something>.append ; I'm not sure what you think this does, but append is a method and it not only must be called, with () , but it needs a parameter: the thing you are appending.

So, for example, this line:

 item = [].append

makes no sense at all; what are you expecting item to be? What are you hoping to append? Surely you just mean item = [] .

In the specific case, the error is because of the superfluous append on the end of the previous line:

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

Again, just remove the append .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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