以下是html的详细信息:
<div class="aaa">
<span class="bbb"></span>
<a data-track-id="Google Map" href="https://www.google.com/maps/dir//18/@1813z" target="_blank" class="ccc">Google Map</a>
</div>
我只想获取18和1813z的纬度和经度,怎么办?
我正在使用两个URL页面在第二页中获取文本。诸如标题或名称之类的其他信息将以文本形式显示,因此我可以使用get_text().
但是在这种情况下,我无法在href之后获得链接。所以请帮忙。
我尝试的代码:
from bs4 import BeautifulSoup
import urllib.request
def get_page(url):
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
mainpage = response.read().decode('utf-8')
return mainpage
mainpage = get_page('https://www.something.com')
mainpage_parser = BeautifulSoup(mainpage,'html.parser')
secondpage = get_page('https://www.something2.com/')
secondpage_parser = BeautifulSoup(secondpage,'html.parser')
try:
for gps in secondpage_parser.find_all('a',{"data-track-id":"Google Map"}, href=True):
gpslocation = gps['href']
print(gpslocation)
except:
print(gpslocation)
答案 0 :(得分:0)
首先,页面是使用JavaScript动态生成的,如果您打印secondpage_parser,您会发现该行
<a data-track-id="Google Map" href="https://www.google.com/maps/dir//18/@1813z" target="_blank" class="ccc">Google Map</a>
在原始源中不存在。您说想要纬度和经度,可以从地图图像属性中获取,例如:
from bs4 import BeautifulSoup
import urllib.request
from urllib.parse import urlparse, parse_qs
def get_page(url):
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
mainpage = response.read().decode('utf-8')
return mainpage
mainpage = get_page('https://www.wongnai.com/businesses?domain=1&page.number=1&sort.type=8®ions=373')
mainpage_parser = BeautifulSoup(mainpage,'html.parser')
secondpage = get_page('https://www.wongnai.com/restaurants/thebarnsteakhousechiangmai?_st=cD0wO2I9Mjc0MzU0O2FkPXRydWU7dD0xNTM2NDE4MzIxOTM1O2k9NDk3MWEyZGQtMDY5Mi00MDU2LWExZmUtZmE5Y2ZlMjVhODYwO3dyZWY9c3I7&wref=sr')
secondpage_parser = BeautifulSoup(secondpage,'html.parser')
try:
for gps in secondpage_parser.find_all('img',{"alt":"map"}, srcset=True):
parsed_url = urlparse(gps['srcset'])
mykeys = ['lat', 'lon']
gpslocation = [parse_qs(parsed_url.query)[k][0] for k in mykeys]
print(gpslocation)
except:
print(gpslocation)
输出:
['18.832687', '98.998473']