【问题标题】:Basic BeautifulSoup Wikipedia scrape基本 BeautifulSoup 维基百科抓取
【发布时间】:2017-04-30 08:48:37
【问题描述】:

我正在尝试从 Wikipedia 获取一个非常基本且简短的基本无序列表 <ul>。我的最终目标是将其放入DataFrame。 我的问题是,我从这里去哪里?

In [28]: from bs4 import BeautifulSoup

         import urllib2

         import requests

         from pandas import Series,DataFrame

In [29]: url = "https://en.wikipedia.org/wiki/National_Pro_Grid_League"

In [31]: result = requests.get(url)

In [32]: c = result.content

In [33]: soup = BeautifulSoup(c)

我似乎无法在这个 StackOverflow 上找到任何答案,所以如果有人能给我任何建议,我将不胜感激。
这是我正在寻找的具体列表:

Active teams[edit]
Baltimore Anthem (2015–present)
Boston Iron (2014–present)
DC Brawlers (2014–present)
Los Angeles Reign (2014–present)
Miami Surge (2014–present)
New York Rhinos (2014–present)
Phoenix Rise (2014–present)
San Francisco Fire (2014–present)

【问题讨论】:

  • 谢谢亲爱的斯宾塞 - 还有乔治 - 这是基于 python 2 的。我需要将其重写为 version3

标签: python pandas web-scraping beautifulsoup


【解决方案1】:

首先,您需要找到页面的正确部分。为此,您可以找到带有 id="Active_teams_at_league_closing" 的标题,然后从那里找到下一个 <ul> 元素。

from bs4 import BeautifulSoup
import requests

url = "https://en.wikipedia.org/wiki/National_Pro_Grid_League"
r = requests.get(url)
soup = BeautifulSoup(r.content)

heading = soup.find(id='Active_teams_at_league_closing')
teams = heading.find_next('ul')
for team in teams:
    print(team.string)

【讨论】:

  • 嗨,亲爱的 CDplayer 和 wpercy - 非常感谢这个很好的例子 - 运行 python 的 v3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 2020-07-20
  • 2013-11-13
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多