[英]Can't scrape nested html using BeautifulSoup
我有兴趣从http://hdsc.nws.noaa.gov/hdsc/pfds/pfds_map_cont.html?Lat=33.146425&Lon=-87.5805543的以下源代码中抓取“0.449”。
<td class="tblInner" id="0-0">
<div style="font-size:110%">
<b>0.449</b>
</div>
"(0.364-0.545)"
</td>
使用 BeautifulSoup,我目前已经写了:
storm=soup.find("td",{"class":"tblInner","id":"0-0"})
这导致:
<td class="tblInner" id="0-0">-</td>
我不确定为什么嵌套在 td 中的所有内容都没有出现。 当我搜索 td 的内容时,我的结果只是“-”。 如何从这段代码中获取我想要的值?
您可能正在抓取一个在初始加载后使用 javascript 更新 DOM 的网站。
你有几个选择:
python headless browser
)。编辑:
我没有看到您发布了您要抓取的网址。
在您的特定情况下,您想要的数据来自对此 URL 的 AJAX 调用:
您现在只需要了解每个参数的作用,并解析其输出而不是编写 HTML抓取工具。
请原谅缺乏错误检查和模块化,但这应该可以满足您的需求,基于@Eloims 观察:
import requests
import re
url = 'http://hdsc.nws.noaa.gov/cgi-bin/hdsc/new/cgi_readH5.py?lat=33.1464&lon=-87.5806&type=pf&data=depth&units=english&series=pds'
r = requests.get(url)
response = r.text
coord_list_text = re.search(r'quantiles = (.*);', response)
coord_list = eval(coord_list_text.group(1))
print coord_list[0][0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.