简体   繁体   中英

How to get HTML with expanded containers using Python?

When I get HTML of the page, eg

response = urllib2.urlopen('http://www.wunderground.com/us/fl/miami/precipitation')
html = response.read()

I get HTML with collapsed containers, eg

<h2>6-Hour Precipitation Forecast</h2>
<div id="precip-statement"></div>
<div id="precip-graph">

while the real HTML looks like that:

在此处输入图片说明

Clearly, I need to extract 6-hour forecast, which I cannot do having it collapsed into <div id="precip-statement"></div>

I will be very thankful if you can help me with this issue. Thank you

The content is loaded dynamically using ajax. You can sniff this request with Chrome. Press F12 -> Network -> XHR and look at requests, one of them (wwir.json) returns a nice json that you can parse using:

 import json
 weather = json.loads(response)

It looks like they use API key from api.weather.com, which probably means you should get your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM