【问题标题】:Scraping a list of events from Wikipedia从维基百科抓取事件列表
【发布时间】:2020-07-24 16:09:48
【问题描述】:

我正在使用 BS4 抓取 wiki 页面以获取每月事件 我要查找的数据不是存储在表中,而是存储在列表中。 如何解析、清理并将其转换为表格?

这是我尝试过的

url = "https://en.wikipedia.org/wiki/Portal:Current_events/October_2019"

html = urlopen(url)

soup = BeautifulSoup(html,'html.parser')

print(soup.prettify())

all_events=soup.find_all("li")

all_events

pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

tableofevents = pd.DataFrame(all_events)

tableofevents

目前没有工作... 感谢您提供任何提示或技巧。

R

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    试试这个:

    import requests
    from bs4 import BeautifulSoup
    base_site = "https://en.wikipedia.org/wiki/Portal:Current_events/October_2019"
    response = requests.get(base_site)
    html = response.content
    soup = BeautifulSoup(html, "html.parser")
    links = soup.find_all('a')
    empty = []
    for i in links:
      try:
        empty.append(i['href'])
      except:
        print('link not found')
    import pandas as pd
    table = pd.DataFrame(empty)
    print(table)
    

    检查这个 colab 笔记本: Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 2019-07-20
      • 2020-07-20
      • 1970-01-01
      • 2016-09-08
      • 2017-04-30
      • 2020-07-16
      • 1970-01-01
      相关资源
      最近更新 更多