【发布时间】: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