一个搜索网站https://std.samr.gov.cn/,一个excel表格,根据表格(共1列,每行1格),依次在网站搜索,结果唯一,下载文件
时间: 2024-03-12 09:47:32 浏览: 32
好的,这是一个自动化的任务,需要使用Python编程语言来实现。具体的思路如下:
1. 读取Excel表格中的数据,保存到一个列表中。
2. 使用Selenium模块打开Chrome浏览器,并访问给定的网址。
3. 在搜索框中输入列表中的每个关键词,并点击搜索按钮。
4. 找到搜索结果中唯一的元素,并点击下载链接。
5. 下载文件到本地目录中。
下面是一个简单的Python程序,可以实现上述任务:
```python
import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
# 读取Excel表格中的数据
data = []
with open('data.xlsx', 'r') as f:
for line in f.readlines():
data.append(line.strip())
# 设置Chrome浏览器的路径和驱动程序路径
chrome_path = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
driver_path = 'chromedriver.exe'
# 启动Chrome浏览器
service = Service(driver_path)
service.start()
options = webdriver.ChromeOptions()
options.binary_location = chrome_path
driver = webdriver.Chrome(service=service, options=options)
# 访问搜索网站
driver.get('https://std.samr.gov.cn/')
# 依次搜索并下载文件
for keyword in data:
# 输入关键词并搜索
search_box = driver.find_element(By.ID, 'query_text')
search_box.clear()
search_box.send_keys(keyword)
search_box.send_keys(Keys.RETURN)
# 等待搜索结果加载完成
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'search_result')))
# 找到唯一的搜索结果并下载文件
result_links = driver.find_elements(By.CLASS_NAME, 'search_result_title')
if len(result_links) == 1:
result_links[0].click()
time.sleep(3)
download_link = driver.find_element(By.XPATH, '//a[text()="下载"]')
download_url = download_link.get_attribute('href')
os.system('wget -P downloads/ ' + download_url)
# 关闭浏览器
driver.quit()
```
在这个程序中,我们使用了Selenium模块来自动化浏览器操作,使用了os模块来下载文件。需要注意的是,程序中的Chrome浏览器和驱动程序的路径需要根据实际情况进行修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)