[英]Python 3 Beautiful Soup 4 .findall not working
我是python和beautifulsoup的新手,如果这是一个愚蠢的问题,我很抱歉。 我试图创建一个webscraper ,它采用输入国家的名称,并从以下网站 ( https://tradingeconomics.com/country-list/money-supply-m2 ) 找到其货币供应量。 每当我尝试查找所有 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()? "
每当我删除 .find_all(a) 时它都会起作用。
这是我的代码:
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()
有人能告诉我我做错了什么吗?
谢谢你。
find_all
返回(本质上)一个列表。 列表没有find_all
方法。
您需要遍历所有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.