简体   繁体   English

尝试从按钮获取纬度和经度数据

[英]Trying to get latitude and longitude data from button

I'm trying to scrape the latitude and longitude data from "Show Map" button on this result page: https://www.psychologytoday.com/us/therapists/60148/374863?sid=5d01e84909804&ref=2&tr=ResultsName 我正在尝试从此结果页面上的“显示地图”按钮抓取纬度和经度数据: https : //www.psychologytoday.com/us/therapists/60148/374863?sid=5d01e84909804&ref=2&tr=ResultsName

Here's what I have tried so far= 到目前为止,这是我尝试过的=

button = soup.find('button', {"data-event-label":'Address_MapButton'})
print(button['data-map-lat'])

Full code: import requests from bs4 import BeautifulSoup from bs4.element import Tag 完整代码:从bs4导入请求从bs4.element import标签导入BeautifulSoup

zipcode = int(input("Zipcode: "))
url = 'https://www.psychologytoday.com/us/therapists/{0}'.format(zipcode)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
result = soup.find(class_='results-column')

addressArray = []

for tag in result:
    if isinstance(tag,Tag):
        _class = tag.get("class")

        if _class is None or _class is not None and "row" not in _class:
            continue

        link = (tag.find(class_='result-actions')).find('a',href=True)

        _href = link['href']

        address_link = requests.get(_href, headers=headers)
        soup1 = BeautifulSoup(address_link.text, 'html.parser')

        address = (soup1.find(class_='address')).find(class_="location-address-phone")
        button = soup.find('button', {"data-event-label":'Address_MapButton'})
        print(button['data-map-lat'])

print(addressArray)

I'm getting a None return. 我得到无回报。 I'd like to see latitude coordinates. 我想查看纬度坐标。

You want to extract the appropriate attributes. 您要提取适当的属性。 I use the first match on class btn-location to get element housing attributes. 我使用btn-location类的第一个匹配项来获取元素的住房属性。

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://www.psychologytoday.com/us/therapists/60148/374863?sid=5d01e84909804&ref=2&tr=ResultsName', headers = {'User-Agent': 'Mozilla/5.0'})
soup = bs(r.content, 'lxml')
elem = soup.select_one('.btn-location[data-map-lat]')
lat = elem['data-map-lat']
lon = elem['data-map-lon']
print(lat, lon) 

OP's version: OP的版本:

soup2 = bs(address_link.content, 'lxml') 
elem = soup2.select_one('.btn-location[data-map-lat]') 
lat = elem['data-map-lat'] 
lon = elem['data-map-lon'] 
latArray.append(lat)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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