【问题标题】:web scraping: beautifulsoup网页抓取:beautifulsoup
【发布时间】:2019-11-10 20:11:35
【问题描述】:

我正在尝试使用漂亮的汤和 Python/Pandas 从维基百科页面中提取所有“感兴趣的地方”,并将它们放入数据框中。例如:

https://en.wikipedia.org/wiki/1st_arrondissement_of_Paris

url_Paris_01 = requests.get('https://en.wikipedia.org/wiki/1st_arrondissement_of_Paris').text
soup_Paris_01 = BeautifulSoup(url_Paris_01, "html.parser")

for headline in soup_Paris_01.find_all("span", {"class": "mw-headline"}):
    print(headline.text)

Geography
Demography
Historical population
Immigration
Quarters
Economy
Education
Map
Cityscape
**Places of interest**
Bridges
Streets and squares
See also
References
External links

没用

soup_Paris_01.find_all('li',attrs={"id":"Places_of_interest"}) 

我看到我的“名胜古迹”都有一个标题标签。

名胜古迹

【问题讨论】:

  • 您之前问了几个问题并得到了有用的答案,但您没有接受任何一个,为什么?
  • 嗨。我是stackoverflow的新手。我不确定如何接受他们。我现在就去做。
  • 类似soup_Paris_01.find_all('li', {'class': None}, {'id':'Places_of_interest'}) 的东西有效,但我现在想根据维基百科页面限制我的结果而且“Places_of_interest”也不准确(包含),因为一些维基页面有“Places_of_interest in the arrondissement”

标签: html pandas web-scraping beautifulsoup


【解决方案1】:

首先找到place of interestspan标签下的ul项目,然后对ul项目下的所有锚标签执行find_all()

from bs4 import BeautifulSoup
import requests
url_Paris_01 = requests.get('https://en.wikipedia.org/wiki/1st_arrondissement_of_Paris').text
soup_Paris_01 = BeautifulSoup(url_Paris_01, "html.parser")
placeofinterset=soup_Paris_01.find("span",id="Places_of_interest").find_next('ul')
for place in placeofinterset.find_all('a'):
    print(place['title']) #This will give you title
    print(place.text) #This will give you text

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多