【发布时间】:2015-11-20 06:08:55
【问题描述】:
我想从这个链接中抓取国家名称和国家首都: https://en.wikipedia.org/wiki/List_of_national_capitals_in_alphabetical_order
从 html 代码中,我正在寻找所有这些:
from bs4 import BeautifulSoup
import requests
BASE_URL = "https://en.wikipedia.org/wiki/List_of_national_capitals_in_alphabetical_order"
html = requests.get(BASE_URL).text
soup = BeautifulSoup(html, "html.parser")
countries = soup.find_all("td")
print (countries)
但我不知道如何真正获取标签之间的内容,尤其是因为其中没有任何信息。
我觉得这很简单,但我无法真正理解所有教程,因为它们使用类,而这个 wiki 页面在表格中没有用于其信息的类。
【问题讨论】:
-
您可以使用任何有效的识别特征来选择要提取的内容。也许您应该通过对您尝试操作的页面的简要分析来更新您的问题。一些常见但脆弱的方法是“在页面上查找第三个表格”或“在第一个小节标题之后查找表格”,但也许您可以想出更强大的方法。
标签: python web-scraping beautifulsoup scrape