【发布时间】:2016-07-19 07:28:53
【问题描述】:
我正在尝试使用 BeautifulSoup 从天气网站检索数据。网站示例:
<channel>
<title>2 Hour Forecast</title>
<source>Meteorological Services Singapore</source>
<description>2 Hour Forecast</description>
<item>
<title>Nowcast Table</title>
<category>Singapore Weather Conditions</category>
<forecastIssue date="18-07-2016" time="03:30 PM"/>
<validTime>3.30 pm to 5.30 pm</validTime>
<weatherForecast>
<area forecast="TL" lat="1.37500000" lon="103.83900000" name="Ang Mo Kio"/>
<area forecast="SH" lat="1.32100000" lon="103.92400000" name="Bedok"/>
<area forecast="TL" lat="1.35077200" lon="103.83900000" name="Bishan"/>
<area forecast="CL" lat="1.30400000" lon="103.70100000" name="Boon Lay"/>
<area forecast="CL" lat="1.35300000" lon="103.75400000" name="Bukit Batok"/>
<area forecast="CL" lat="1.27700000" lon="103.81900000" name="Bukit Merah"/>`
</weatherForecast>
<channel>
我想检索介于 validTime
之间的下午 3.30 到下午 5.30从页面检查元素后,我发现下午 3.30 到下午 5.30 在 span 元素内的“class= Text”中:
sample of how the weather data looks like
这是我使用 Python 编写的代码:
import requests
from bs4 import BeautifulSoup
import urllib3
r = requests.get('http://www.nea.gov.sg/api/WebAPI/?
dataset=2hr_nowcast&keyref=781CF461BB6606AD907750DFD1D07667C6E7C5141804F45D')
soup = BeautifulSoup(r.content, "xml")
soup.find('validTime').string
但是,当我运行代码时,我收到一条错误消息“ImportError : No module name requests”
我已经下载了请求并将它们放在in the Lib within Python27 folder on my C drive
我还使用“pip list”检查我是否安装了请求,这就是我得到的:
beautifulsoup4 <4.4.1>
httplib2 <0.9.2>
lxml <3.6.0>
pip <8.1.2>
requests <2.10.0>
...
如您所见,请求已下载到我的 C 盘 (C:\Python27\Scripts)
为什么我在命令提示符下出现错误?
【问题讨论】:
-
您确定在运行 Python 可执行文件时使用了正确的可执行文件吗?您可能已经为一个版本安装了
requests,然后尝试用另一个版本执行代码。 -
如何检查我是否使用了正确的?
-
我的python是2.7.12版本!
标签: python python-requests importerror