- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
from selenium import webdriver
import re
driver= webdriver.Chrome(executable_path=r"C:\Users\chromedriver")
sentence = "chiropractor in maryland"
url="https://google.com/search?hl=en&q={}".format(sentence)
driver.get(url)
links=driver.find_elements_by_xpath('//a[@href]')
maps=[i for i in links if i.text=="Maps"][0].click()
html=driver.page_source
#ChIJaYGxdRj9t4kRcJmJlvQkKX0
#ChIJCf4MzWjgt4kRluBnhQTHlBM
#ChIJBXxr8brIt4kRVE-gIYDyV8c
#ChIJX0W_Xo4syIkRUAtRFy8nz1Y place ids in html
你好,这是我的第一个 selenium 项目,我试图从结果中找到地点 id,我添加了一些地点 id(我使用 API),我尝试在检查器工具中找到它们,但是我找不到,它们可以在我尝试使用正则表达式的页面源中找到,它们似乎遵循以下路径
2,[null,null,\\"bizbuilder:gmb_web\\",[6,7,4,1,3]\\n]\\n]\\n]\\n,1,null,null,null,null,null,null,[\\"-8523065488279764631\\",\\"9018780361702349168\\"]\\n]\\n]\\n]\\n,null,null,null,[[\\"chiropractor\\"]\\n]\\n,null,\\"ChIJaYGxdRj9t4kRcJmJlvQkKX0\\",null,null,null,[\\"South Gate\\",\\"806 Landmark Dr Suite 126\\",\\"806 Landmark Dr Suite 126\\",\\"Glen Burnie\\"]\\n,null,null,null,null,null,[null,\\"SearchResult.TYPE_PERSONAL_
在“\”脊椎按摩师\”之后]\n]\n,null,\"地点 ID”,null ...
但我找不到它的正则表达式。我需要帮助编写正确的正则表达式或找到另一种查找 palce_id 的方法。我希望没有人回答提到使用他们的 API
最佳答案
我认为这可以改进,但字符串本身位于其中包含 window.APP_OPTIONS
的脚本标记中。每个 ID 都以 ChIJ
开头,后面有一个定义的字符集,总长度为 27。
我也直接从 map 页面开始,而不是单击它。尽管运行了几次,但我不需要等待条件。如果需要,可以添加此内容。
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import re
sentence = "chiropractor in maryland"
url = 'https://www.google.com/maps/search/{}'.format(sentence)
d = webdriver.Chrome()
d.get(url)
soup = bs(d.page_source, 'lxml')
for script in soup.select('script'):
if 'window.APP_OPTIONS' in script.text:
script = script.text
break
r = re.compile(r'(ChIJ[a-zA-Z\.0-9\-\_]{23})')
items = r.findall(script)
print(items)
d.quit()
<小时/>
风险较大,您可以直接使用 page_source
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import re
sentence = "chiropractor in maryland"
url = 'https://www.google.com/maps/search/{}'.format(sentence)
d = webdriver.Chrome()
d.get(url)
r = re.compile(r'(ChIJ[a-zA-Z\.0-9\-\_]{23})')
items = r.findall(d.page_source)
print(items)
d.quit()
<小时/>
注释:
我指定一个模式,旨在仅匹配当前所需的项目(对于给定的搜索)。可以想象,在未来/新的搜索中,该模式可能会出现,而不是一个 ID。 page_source 是一个更大的搜索空间,因此更有可能遇到与模式匹配的不需要的字符串。脚本标签不仅是您期望找到 id 的地方,而且也是一个较小的搜索空间。随着时间的推移,您可能还想检查字符集是否不需要任何额外的字符来匹配新的 id。您可以轻松检查每页计数的结果。
关于python - 谷歌地图使用 Selenium 的地点ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55771291/
leaflet:一个开源并且对移动端友好的交互式地图 JavaScript 库 中文文档: https://leafletjs.cn/reference.html 官网(英文): ht
我是一名优秀的程序员,十分优秀!