写一段代码 使用selenim 1,打开大众点评网 2,将鼠标悬浮到美食 3,点击火锅 4,获取商家信息
时间: 2023-03-23 12:04:35 浏览: 89
browsermob-proxy-2.1.4.7z
以下是使用 Selenium 的 Python 代码:
```python
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# 打开浏览器并访问大众点评网
driver = webdriver.Chrome()
driver.get("https://www.dianping.com/")
# 找到美食标签并悬浮鼠标
food_tag = driver.find_element_by_xpath("//div[@class='nav-primary']/ul/li[1]/a")
ActionChains(driver).move_to_element(food_tag).perform()
# 找到火锅标签并点击
hotpot_tag = driver.find_element_by_xpath("//div[@class='nav-sub']/ul/li[1]/a")
hotpot_tag.click()
# 获取商家信息
shops = driver.find_elements_by_xpath("//div[@class='shop-list J_shop-list shop-all-list']/ul/li")
for shop in shops:
name = shop.find_element_by_xpath(".//div[@class='tit']/a/h4/text()")
address = shop.find_element_by_xpath(".//div[@class='tag-addr']/span[@class='addr']/text()")
print(name, address)
# 关闭浏览器
driver.quit()
```
阅读全文