[英]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.