Соскоб сети: Beautifulsoup - PullRequest
       4

Соскоб сети: Beautifulsoup

0 голосов
/ 10 ноября 2019

Я пытаюсь извлечь все "Интересные места" со страниц Википедии, используя красивый суп и 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"}) 

Я вижу, что мои "Интересные места"у всех есть заголовок.

Достопримечательности

1 Ответ

2 голосов
/ 10 ноября 2019

Сначала найдите элемент ul в теге place of interest span, а затем выполните find_all () для всех тегов привязки в элементе ul.

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...