我对beauthulsoup4相当陌生,从下面的代码中提取html响应中的纬度和经度值时遇到了困难。在
url = 'http://cinematreasures.org/theaters/united-states?page=1'
r = requests.get(url)
soup = BeautifulSoup(r.content)
links = soup.findAll("tr")
print links
这个代码多次打印出这个响应。在
^{pr2}$全tr响应
<tr>\n
<th id="theater_name"><a href="/theaters/united-states?sort=name&order=desc">\u2191 Name</a>
</th>\n
<th id="theater_location"><a href="/theaters/united-states?sort=location&order=asc">Location</a>
</th>\n
<th id="theater_status"><a href="/theaters/united-states?sort=open&order=desc">Status</a>
</th>\n
<th id="theater_screens"><a href="/theaters/united-states?sort=screens&order=asc">Screens</a>
</th>\n</tr>,
<tr class="even location theater" data="{id: 0, point: {lng: -94.1751038, lat: 36.0848965}, category: 'open'}">\n
<td class="name">\n
<a class="map-link" href="/theaters/8775">
<img alt="112 Drive-In" height="48" src="http://photos.cinematreasures.org/production/photos/22137/1313612883/thumb.JPG?1313612883" width="48" />
</a>\n<a class="map-link" href="/theaters/8775">112 Drive-In</a>\n
<div class="info-box">\n
<div class="photo" style="float: left;">
<a href="/theaters/8775">
<img alt="thumb" height="48" src="http://photos.cinematreasures.org/production/photos/22137/1313612883/thumb.JPG?1313612883" width="48" />
</a>
</div>\n
<p style="min-width: 200px !important;">\n<strong><a href="/theaters/8775">112 Drive-In</a></strong>\n
<br>\n 3352 Highway 112 North
<br>Fayetteville, AR 72702
<br>United States
<br>479.442.4542
<br>\n</br>
</br>
</br>
</br>
</br>
</p>\n</div>\n</td>\n
<td class="location">\n Fayetteville, AR, United States\n</td>\n
<td class="status">\n Open\n</td>\n
<td class="screens">\n 1\n</td>\n</tr>
我该如何从该响应中获取lng和lat值?在
提前谢谢你。在
如果您只希望得到一个响应,请执行以下操作:
好的,所以您正确地获取了所有
<tr>
,现在我们只需要从它们中获取data属性。在不幸的是,这给了您一个字符串列表,而不是一个人们可能希望的dictionary对象。我们可以在数据字符串上使用正则表达式将其转换为dict(谢谢RootTwo):
^{pr2}$我的方法是:
我不使用任何字符串操作。相反,我从
^{pr2}$data
属性加载JSON。不幸的是,这里不是很有效的JSON,所以我使用demjson
库进行JSON解析。在相关问题 更多 >
编程相关推荐