使用百度api的时候有次数限制,爬虫就不能很好的用到,这时候就直接使用自动化操作就好
优点:没有限制
缺点:速度相对爬虫慢得多
代码:
# 浏览器功能
import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://api.map.baidu.com/lbsapi/getpoint/index.html')
driver.maximize_window()
time.sleep(3)
# 通过按钮的xpath定位元素
button = driver.find_element(By.XPATH, "/html/body/div[1]/div[2]/ul/li[1]/div/span/a")
# 点击按钮
button.click()
time.sleep(3)
# 定位输入框元素
input_element = driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div[2]/table/tbody/tr[1]/td/input[1]")
#找到赣州市#############
# 点击输入框
input_element.click()
# 清空输入框
input_element.clear()
# 输入赣州市
input_element.send_keys("赣州市")
time.sleep(3)
# 点击确认
# 等待按钮出现并点击
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[1]/div[2]/table/tbody/tr[1]/td/input[2]"))).click()
time.sleep(2)
data = ["章贡区政府机构","章贡区医院","章贡区超市",'章贡区酒店', '章贡区住宅小区', '章贡区公园', '章贡区餐馆', '章贡区村庄', '章贡区学校', '章贡区工厂', '章贡区加油站', '章贡区办公大厦']
# 创建CSV文件并写入表头
with open('locations.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['名称', '经度', '纬度'])
for item in data:
# 输入地点名称
input_element2 = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/input[1]")
input_element2.click()
input_element2.clear()
input_element2.send_keys(item)
time.sleep(3)
# 点击搜索按钮
search_button = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[2]/input[2]")
search_button.click()
time.sleep(2)
# 循环获取每一页的数据
while True:
for i in range(0, 10):
# 点击地点信息
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, f"no_{i}"))).click()
time.sleep(3)
# 获取地点名称、经度和纬度
location_name = driver.find_element(By.XPATH, f"//*[@id='no_{i}']//a").text
print(location_name)
location_info = driver.find_element(By.XPATH, f"//*[@id='no_{i}']").text
#print(location_info)
longitude, latitude = location_info.split('坐标:')[1].split(',')
print(longitude,latitude)
# 写入CSV文件
writer.writerow([location_name, longitude, latitude])
time.sleep(3)
# 点击下一页按钮
try:
next_page_button = driver.find_element(By.XPATH, "/html/body/div[1]/div[3]/div[2]/div/div/p/span[5]/a")
next_page_button.click()
time.sleep(3)
except:
break
# 关闭浏览器
driver.quit()