【问题标题】:Python Scrape of Wikipedia table then export to csvPython Scrape of Wikipedia table 然后导出到 csv
【发布时间】:2019-11-01 12:14:42
【问题描述】:

我已按照教程抓取表格,然后将数据导出到 csv 文件。当我尝试执行文件说

时,我通过 PyCharm 收到错误

" 回溯(最后一次调用): 文件“I:/Scrape/MediumCode.py”,第 1 行,在 导入请求 ModuleNotFoundError:没有名为“请求”的模块“

我还假设代码及其逻辑中存在其他错误,但这是我遇到的第一个问题,如果不理解为什么无法识别库,就无法进一步解决

成功运行 pip 安装请求

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://en.wikipedia.org/wiki/Public_holidays_in_Switzerland'

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("table", {"class":"wikitable"})

filename = "holidays.csv"
f = open(filename, "w")

headers = "holiday, holiday_date"

f.write(headers)

for container in containers:
    holiday = container.table.tbody.tr.td.a["title"]

    name_container = container.findAll("a", {"class":"title"})
    holiday_name = name_container[0].text

    date_container = container.findAll("td")
    date = date_container[0].text.strip()

    print("holiday: " + brand)
    print("holiday_name: " + holiday_name)
    print("date: " + date)

    f.write(holiday + "," + holiday_name.replace(",", "|") + "," + date + "\n")

    f.close()

【问题讨论】:

  • 在第一行添加导入请求时出现同样的错误
  • 尝试在一个完全独立的文件中运行import requests 行,看看是否会遇到同样的错误。
  • 没有输出同样的错误

标签: python web-scraping beautifulsoup python-requests export-to-csv


【解决方案1】:

使用pandas 库

  • .read_html() - 将 HTML 表格读入 DataFrame 对象列表。
  • .to_csv() - 将对象写入逗号分隔值 (csv) 文件。

import requests
import pandas as pd

url = 'https://en.wikipedia.org/wiki/Public_holidays_in_Switzerland'
response = requests.get(url)

tables = pd.read_html(response.text)

# write holiday table data into `holiday_data` csv file
tables[0].to_csv("holiday_data.csv")

安装熊猫库

pip3 install pandas

如果requests 库仍未在您的系统中引发错误,请尝试以下操作:

from urllib.request import urlopen as uReq
import pandas as pd

url = 'https://en.wikipedia.org/wiki/Public_holidays_in_Switzerland'
response = uReq(url)
tables = pd.read_html(response.read())
#select only holiday column
select_table_column = ["Holiday"]
'''
    #or select multiple columns 
    select_table_column = ["Holiday","Date"]

'''
# filter table data by selected columns
holiday = tables[0][select_table_column]

# # write holiday table data into `holiday_data` csv file and set csv header
holiday.to_csv("holiday_data.csv",header=True)

【讨论】:

  • 请求仍然在您发布的第一个代码中引发错误。您发布的第二段代码现在没有任何错误!谢谢!!
  • 现在,如果我只想获取第一个选择列,我将如何遍历它们?
  • 你能把这两段代码合并在一起吗?我正在用更新的代码替换表 [0].to_csv("holiday_data.csv") 并且遇到问题
  • @ERich ANswer 更新,检查一下。
【解决方案2】:

使用您的代码,我可以获得page_html 就好了。所以你的系统出于某种原因不喜欢urllib.request。请注意request 与requests 并不完全相同。据我了解,requests 建立在urllib3 之上,而urllib.request 在标准库中,尽管它们都指向一些常见的东西。

这段代码对你有用吗?

from urllib import request

my_url = 'https://en.wikipedia.org/wiki/Public_holidays_in_Switzerland'
p = request.urlopen(my_url)
print(p.read())

【讨论】:

  • 从 PyCharm 切换到 Anaconda/Spyder,是的,它确实有效。在 Spyder IDE 中运行我上面的代码现在会抛出这些错误 Traceback(最近一次调用最后): File "", line 1, in runfile('C:/Users/Riche10/ .spyder-py3/Test_Scraper.py', wdir='C:/Users/Riche10/.spyder-py3')
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2019-07-13
  • 2023-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多