【发布时间】:2023-04-06 03:19:01
【问题描述】:
我是网络抓取的新手,我正在尝试从该网站提取所有期刊的所有名称:https://ideas.repec.org/top/top.journals.simple.html。
这是我目前的尝试(按照这里的教程https://www.pluralsight.com/guides/extracting-data-html-beautifulsoup):
import requests
from bs4 import BeautifulSoup
URL = "https://ideas.repec.org/top/top.journals.simple.html"
html_content = requests.get(URL).text
soup = BeautifulSoup(html_content, "lxml"
journal_list = soup.find("table", attrs={"class": "toplist"})
journal_list_data = journal_list.tbody.find_all("tr")
headings = []
for td in journal_list_data[0].find_all("td"):
headings.append(td.b.text.replace('\n', '').strip())
print(headings)
这只是为了获取表格标题的列表,然后我会尝试从“期刊”列中提取所有期刊名称,但我得到一个 AttributeError 基本上说 journal_list.tbody 是 NoneType,当我检查 journal_list.attrs 时,它只给出 {'class': 'toplist'),即使页面上的 HTML 肯定有一个 tbody 属性。
我做错了什么/还有其他更好的方法吗?
谢谢!
【问题讨论】:
标签: python html beautifulsoup