用selenium访问信用中国,检索企业信用信息详情截图保存到word和下载企业信用报告,总是被拦截,求指导,感谢
43条回答 默认 最新
檀越@新空间 2025-01-06 01:56关注获得0.80元问题酬金 上午好☀️☀️☀️️
本答案参考通义千问要使用Selenium访问“信用中国”网站,并检索企业信用信息详情,然后将详情截图保存到Word文档以及下载企业信用报告,可能会遇到一些反爬虫机制。以下是一些可能的解决方案:
-
使用合适的浏览器驱动:
- 确保你使用的WebDriver版本与你的浏览器版本匹配。
- 定期更新WebDriver以确保兼容性。
-
模拟人类行为:
- 添加随机等待时间以避免被检测为自动化脚本。
- 使用ActionChains模拟鼠标移动和点击操作。
-
设置合理的User-Agent:
- 设置一个常见的User-Agent字符串,使请求看起来像是来自普通浏览器。
-
绕过验证码:
- 如果网站有验证码,可以尝试使用第三方验证码识别服务(如:2Captcha、Anti-Captcha)。
- 或者使用OCR技术来识别验证码。
-
使用代理IP:
- 使用代理IP池来避免频繁请求导致的IP被封禁。
-
处理弹窗和警告:
- 在执行操作前,确保关闭任何可能弹出的警告或弹窗。
-
保存截图到Word文档:
- 使用Python的
python-docx库来创建Word文档并将图片插入其中。
- 使用Python的
-
下载文件:
- 设置WebDriver选项来自动下载文件,或者通过HTTP请求直接下载文件。
解决方案
步骤1: 安装必要的库
pip install selenium python-docx步骤2: 编写代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options from docx import Document import time import os # 配置Chrome WebDriver chrome_options = Options() chrome_options.add_argument("--disable-notifications") chrome_options.add_experimental_option("prefs", { "download.default_directory": os.getcwd(), "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True }) driver = webdriver.Chrome(options=chrome_options) # 访问信用中国网站 driver.get("https://www.creditchina.gov.cn/") # 模拟输入企业名称并搜索 search_box = driver.find_element(By.ID, 'searchKey') search_box.send_keys('示例企业') # 替换为你要查询的企业名称 search_button = driver.find_element(By.XPATH, '//button[@class="btn btn-primary"]') search_button.click() # 等待页面加载完成 WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[@class="search-result-list"]/div'))) # 截图并保存到Word文档 time.sleep(2) # 防止页面未完全加载 driver.save_screenshot('screenshot.png') document = Document() document.add_picture('screenshot.png') document.save('credit_report.docx') # 下载企业信用报告 report_link = driver.find_element(By.XPATH, '//a[contains(text(), "信用报告")]') ActionChains(driver).move_to_element(report_link).click().perform() # 关闭浏览器 driver.quit()注意事项
- 确保替换示例代码中的企业名称和其他元素选择器。
- 根据实际页面结构调整XPath表达式。
- 如果遇到验证码问题,可以考虑使用第三方服务或OCR工具。
希望这些步骤能够帮助你成功地使用Selenium访问“信用中国”网站,并完成所需的操作。
解决 无用评论 打赏 举报-