如何在 beautifulsoup 的多个列表中获取特定元素?

How to take specific elements on multiple lists on beautifulsoup?

我无法提取一些特定的标签(及其字符串内容)并将它们存储到变量中(这样我可以稍后将这些变量放入 CSV 文件中)。

from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/profile/DFletcher1990/')
r.html.render(sleep=5)
soup=BeautifulSoup(r.html.html,'html.parser')

user_info_table=soup.find('table', class_='user-statistics-table')

for tr in user_info_table.find_all('tr'):
    tds=tr.find_all('td')
    print(tds)

我要收藏:

我真的不明白 bs4.element.ResultSet 的行为方式...

你可以把它当作一个列表。

from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.khanacademy.org/profile/DFletcher1990/')
r.html.render(sleep=10)
soup=BeautifulSoup(r.html.html,'html.parser')
user_info_table=soup.find('table', class_='user-statistics-table')
dates,points,videos=[tr.find_all('td')[1].text for tr in user_info_table.find_all('tr')]
print(dates,points,videos,sep="\n")

输出

4 years ago
932,915
372