Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium的GoogleMapsPlaceID_Python_Regex_Web Scraping - Fatal编程技术网

Python 使用selenium的GoogleMapsPlaceID

Python 使用selenium的GoogleMapsPlaceID,python,regex,web-scraping,Python,Regex,Web Scraping,您好,这是我的第一个selenium项目,我正在尝试从结果中查找位置id,我添加了一些位置id(我使用API获得),我尝试在inspector工具中查找它们,但无法找到,但是,它们在我尝试使用regex的页面源代码中可用。它们似乎遵循以下路径 from selenium import webdriver import re driver= webdriver.Chrome(executable_path=r"C:\Users\chromedriver") sentence = "chiropra

您好,这是我的第一个selenium项目,我正在尝试从结果中查找位置id,我添加了一些位置id(我使用API获得),我尝试在inspector工具中查找它们,但无法找到,但是,它们在我尝试使用regex的页面源代码中可用。它们似乎遵循以下路径

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
在“\”脊医\“]\n]\n,null,\“位置ID”之后,null

但是我找不到它的正则表达式。 我需要帮助编写正确的正则表达式或找到另一种查找palce_id的方法。
我希望没有人回答关于使用API的问题,我认为这是可以改进的,但是字符串本身位于脚本标记中,其中包含
窗口。APP_OPTIONS
。每个ID都以
ChIJ
开头,后面有一个定义的字符集,总长度为27

我也直接从地图页面开始,而不是点击它。尽管运行了几次,我不需要等待条件。如果需要,可以添加

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_

风险更大一点,你可以直接从第二页开始

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()

注:


我正在指定一个模式,该模式设计为仅匹配当前所需的项目(对于给定搜索)。可以想象,在未来/新的搜索中,该模式可能会出现,而不是id。page_源是一个更大的搜索空间,因此遇到与该模式匹配的不需要的字符串的可能性更大。脚本标记不仅是您希望找到id的位置,而且是一个更小的搜索空间。随着时间的推移,您可能会因此,想要检查字符集不需要任何额外的字符来匹配新ID。您可以轻松地根据每页计数的结果进行检查。

这两种方法都非常有效,您在搜索页面源时说的风险更大是什么意思?我指定的模式恰好与当前所需的项目匹配。这是一个非常重要的问题ivable该模式可能出现,而不是id。page_源是一个更大的搜索空间,因此遇到与该模式匹配的不需要的字符串的可能性更大。脚本标记不仅是您希望找到id的地方,也是一个更小的搜索空间。
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()