简体   繁体   English

Python 3 Beautiful Soup 4 .findall 不起作用

[英]Python 3 Beautiful Soup 4 .findall not working

I'm new to python and beautifulsoup, I'm sorry if this is a stupid question.我是python和beautifulsoup的新手,如果这是一个愚蠢的问题,我很抱歉。 I was trying to create a webscraper that takes the name of an inputted country and finds its money supply from the following website ( https://tradingeconomics.com/country-list/money-supply-m2 ).我试图创建一个webscraper ,它采用输入国家的名称,并从以下网站 ( https://tradingeconomics.com/country-list/money-supply-m2 ) 找到其货币供应量。 Whenever I try to find all the a tags it keeps giving me this error:每当我尝试查找所有 a 标签时,它都会给我这个错误:

"AttributeError: ResultSet object has no attribute 'find_all'. 
You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()? "

Whenever I remove the .find_all(a) it works.每当我删除 .find_all(a) 时它都会起作用。

Here is my code:这是我的代码:

from bs4 import BeautifulSoup
import requests



url4='https://tradingeconomics.com/country-list/money-supply-m2'
def run_scraper():
  print('running')
  country=input('Please input the name of the country: ')
  
  content4=requests.get(url4).text
  soup4=BeautifulSoup(content4,'html.parser')
  t=country.title()


  table=soup4.find('table',{'class':'table table-hover'})
  for tr in table.find_all('tr'):
    co=tr.find_all('td').find_all('a')
    print(co)
      

run_scraper()

Can someone tell me what I am doing wrong?有人能告诉我我做错了什么吗?

Thank you.谢谢你。

find_all returns (essentially) a list. find_all返回(本质上)一个列表。 A list does not have a find_all method.列表没有find_all方法。

You need to iterate over all td elements, just like you already iterate over all the tr elements:您需要遍历所有td元素,就像您已经遍历所有tr元素一样:

for tr in table.find_all('tr'):
    for td in tr.find_all('td'):
        co = td.find_all('a')
        print(co)

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

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