【发布时间】:2016-05-07 15:23:53
【问题描述】:
我正在制作一个 python 脚本,它将通过搜索关键字温度从谷歌获取温度。 我发现温度值存储在此检查元素代码中的 span id="wob_tm" 中->
<div>
<div class="vk_bk sol-tmp" style="float:left;margin-top:-3px;font-size:64px"><span id="wob_tm" class="wob_t" style="display:inline">
18
</span><span id="wob_ttm" class="wob_t" style="display:none"> … </span>
</div>
可以看出温度 18 在 span id="wob_tm" 内。 所以,我的python脚本是->
from bs4 import BeautifulSoup
import requests,sys,webbrowser
str="temperature"
res = requests.get('http://google.com/search?q=%s'%str)
res.raise_for_status()
examplesoup= BeautifulSoup(res.text,"lxml")
linkelems=examplesoup.findAll("span",{"id":"wob_tm"})
print linkelems.string.strip()
它给了我这个错误- AttributeError:“NoneType”对象没有属性“字符串” 如何纠正它?这意味着链接元素没有元素。
【问题讨论】:
-
为什么要打印链接元的长度?
-
只是为了确保列表链接元素具有要从中提取文本的内容。但奇怪的是,它没有元素。
-
为什么不使用简单的免费天气 API 而不是抓取 google 页面?
-
因为自己制作东西感觉很好。
-
在天气页面我得到它有这个跨度 id =wob_tm。
标签: python beautifulsoup python-requests lxml