用python Selinium清除地图下网站的数据

2024-03-28 10:04:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个要求,从一个网页和数据从地图,如果有一个在网页地图的所有网址。你知道吗

我可以使用Python Selinum从网页中获取所有的url,但无法获取地图下的数据。你知道吗

这是示例URL。在谷歌上,我搜索了“工具租赁”,得到了以下网址。在下面的网页有一个地图,我试图得到地图下的数据。你知道吗

***网址:***

https://www.google.com/search?q=tool+rental&rlz=1C1GCEA_en&oq=tool+rental&aqs=chrome.0.0l6.1735j0j7&sourceid=chrome&ie=UTF-8

***地图:***

enter image description here

所需输出:

The Rental Center at The Home Depot
Northside Tool Rental
Northside Tool Rental

我尝试了不同的选项(通过xpath查找元素、查找元素等)来获取上面的数据,但没有任何效果。你知道吗

下面的代码是我用来从地图中获取url和数据的。你知道吗

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By


driver = webdriver.Firefox(executable_path=r'C:\Chaitu\Projects\Media Marketing\brightlocal\geckodriver.exe')
driver.get("https://www.google.com/search?q=tool+rental&rlz=1C1GCEA_en&oq=tool+rental&aqs=chrome.0.0l6.1735j0j7&sourceid=chrome&ie=UTF-8")
driver.save_screenshot('image.png')

element=driver.find_element_by_tag_name('body')
element_png = element.screenshot_as_png
with open("test2.png", "wb") as file:
    file.write(element_png)


elems = driver.find_elements_by_xpath("//a")
for elem in elems:
    print str(elem.get_attribute("href"))
with open("url_list.txt",'w') as f:
    for elem in elems:
        if 'maps' in str(elem.get_attribute("href")) or ('google' not in str(elem.get_attribute("href")) and 'blogger' not in str(elem.get_attribute("href")) and 'youtube' not in str(elem.get_attribute("href")) and 'https' in str(elem.get_attribute("href"))):
            f.write(str(elem.get_attribute("href"))+"\n")

Tags: 数据in网页getpngdriver地图attribute