1
我对BeautifulSoup4相当新,并且无法从下面的代码中的HTML响应中提取经度和纬度值。如何刮美丽的汤的经纬度
url = 'http://cinematreasures.org/theaters/united-states?page=1'
r = requests.get(url)
soup = BeautifulSoup(r.content)
links = soup.findAll("tr")
print links
此代码多次打印此响应。
<tr class="even location theater" data="{id: 0, point: {lng: -94.1751038, lat: 36.0848965}
完全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和纬度值超出这个回应的?
预先感谢您。
你能给我们提供你想要的网址吗?或者至少是“
除了@wilbur所说的,你需要使用正则表达式来获取所提供示例中表格行中的各个值。 – bmcculley
我编辑原始帖子,正则表达式是唯一的方法? – sbell423
回答
好了,你抓住所有的
<tr>
小号正确,现在我们只需要从他们每个人得到的数据属性。不幸的是,这给你一个字符串的列表,而不是像可能希望的字典对象。我们可以使用数据串正则表达式来将它们转换为类型的字典(感谢RootTwo):
来源
2016-02-25 21:56:20 wpercy
是的,这很好,但字典会是理想的。感谢Wilbur。 – sbell423
'dict(re.findall(r'(lat | lng):\ s *( - ?\ d {1,3} \。\ d +)',data))'会返回一个字典。 – RootTwo
@RootTwo _THANK YOU_!我不是一个足够有才华的正则表达式,但是完全正确。 – wpercy
如果您期望只有一个响应做:
来源
2016-02-25 21:54:01 rye
这里是我的方法:
我不使用任何字符串操作。相反,我从
data
属性加载JSON。不幸的是,这里不是很有效的JSON,所以我使用demjson
库进行json解析。来源
2016-02-25 22:37:58 irvind
不错!我以前没有听说过demjson,我非常喜欢这个解决方案。 – bmcculley
相关问题