本文介绍了如何在美丽的汤中刮擦经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对BeautifulSoup4相当陌生,无法从下面的代码的html响应中提取纬度和经度值.

I am fairly new to BeautifulSoup4 and am having trouble extracting latitude and longitude values out of an html response from the below code.

url = 'http://cinematreasures.org/theaters/united-states?page=1'
r = requests.get(url)
soup = BeautifulSoup(r.content)
links = soup.findAll("tr")
print links

此代码多次打印出此响应.

This code prints out this response multiple times.

<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&amp;order=desc">\u2191 Name</a>
  </th>\n
  <th id="theater_location"><a href="/theaters/united-states?sort=location&amp;order=asc">Location</a>
  </th>\n
  <th id="theater_status"><a href="/theaters/united-states?sort=open&amp;order=desc">Status</a>
  </th>\n
  <th id="theater_screens"><a href="/theaters/united-states?sort=screens&amp;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值?

How would I go about getting just the lng and lat values out of this response?

谢谢.

推荐答案

好的,所以您正确地获取了所有< tr> ,现在我们只需要从每个他们.

Okay, so you grab all the <tr>s correctly, now we just need to get the data attribute from each of them.

import re
import requests
from bs4 import BeautifulSoup

url = 'http://cinematreasures.org/theaters/united-states?page=1'
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")
theaters = soup.findAll("tr", class_="theater")
data = [ t.get('data') for t in theaters if t.get('data') ]
print data

不幸的是,这为您提供了一个字符串列表,而不是像您期望的那样的字典对象.我们可以在数据字符串上使用正则表达式将其转换为dict(感谢 RootTwo ):

Unfortunately this gives you a list of strings, not a dictionary object like one might've hoped for. We can use regular expressions on the data strings to convert them to dicts (thanks RootTwo):

coords = []
for d in data:
    c = dict(re.findall(r'(lat|lng):\s*(-?\d{1,3}\.\d+)', d))
    coords.append(c)

这篇关于如何在美丽的汤中刮擦经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:51