找回密码
 立即注册
首页 业界区 安全 基于selenium的网页自动搜索

基于selenium的网页自动搜索

姚梨素 7 小时前
第一节

通过简单的百度网页打开学习selenium库的基本功能。
  1. 1 from selenium import webdriver
  2. 2 from selenium.webdriver.chrome.service import Service
  3. 3 from selenium.webdriver.chrome.options import Options
  4. 4 from selenium.webdriver.common.by import By
  5. 5 from selenium.webdriver.support.ui import WebDriverWait
  6. 6 from selenium.webdriver.support import expected_conditions as EC
  7. 7 import time
  8. 8
  9. 9 from selenium.webdriver.common.keys import Keys
  10. 10
  11. 11 # 配置 ChromeDriver 路径,替换为你的 ChromeDriver 路径,你也可以将chromedriver拖入文件根目录,使用'./chromedriver.exe'路径。
  12. 12 chrome_driver_path = 'E:/Git_repo/VSCode_tmp/autopython/chromedriver-win64/chromedriver.exe'  # 替换为你的 ChromeDriver 路径
  13. 13
  14. 14 # 初始化 ChromeDriver Service
  15. 15 service = Service(chrome_driver_path)
  16. 16 # 打开浏览器时的相关配置,可以根据需求进行打开和关闭
  17. 17 options = Options()
  18. 18 options.add_argument("--start-maximized")  # 启动时最大化窗口
  19. 19 options.add_argument("--disable-blink-features=AutomationControlled")  # 使浏览器不显示自动化控制的信息
  20. 20 # options.add_argument("--disable-gpu")  # 禁用GPU硬件加速
  21. 21 options.add_argument("--disable-infobars")  # 隐藏信息栏
  22. 22 # options.add_argument("--disable-extensions")  # 禁用所有扩展程序
  23. 23 # options.add_argument("--disable-popup-blocking")  # 禁用弹出窗口拦截
  24. 24 # options.add_argument("--incognito")  # 启动无痕模式
  25. 25 # options.add_argument("--no-sandbox")  # 关闭沙盒模式(提高性能)
  26. 26 # options.add_argument("--disable-dev-shm-usage")  # 使用/dev/shm分区以避免共享内存问题
  27. 27 # options.add_argument("--remote-debugging-port=9222")  # 启用远程调试端口
  28. 28 options.add_experimental_option("excludeSwitches", ["enable-automation"])
  29. 29 options.add_experimental_option("useAutomationExtension", False)
  30. 30
  31. 31
  32. 32 # 初始化 WebDriver,并传入 ChromeDriver Service
  33. 33 driver = webdriver.Chrome(service=service, options=options)
  34. 34 # # 防反爬:隐藏 webdriver 特征
  35. 35 # driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => false});")
  36. 36
  37. 37 try:
  38. 38     # 打开百度搜索首页
  39. 39     url = "https://www.baidu.com"
  40. 40     driver.get(url)
  41. 41     # 显式等待,直到搜索框出现
  42. 42     wait = WebDriverWait(driver, 10)
  43. 43     # 通过Xpath表达式获取到百度输入框的html元素,方法也提供By.ID和By.CLASS_NAME的方法,用户可自动尝试
  44. 44     search_box = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='chat-textarea']")))
  45. 45     # 输入搜索关键词
  46. 46     search_box.send_keys("原神"+ Keys.RETURN)
  47. 47     # 模拟按下回车键进行搜索
  48. 48     #search_box.submit()
  49. 49     # 等待搜索结果加载完成
  50. 50     wait.until(EC.presence_of_element_located((By.XPATH, "//div[@id='content_left']")))
  51. 51     # 打印搜索结果的标题
  52. 52     print(driver.title)
  53. 53     # 延时五秒展示结果
  54. 54     time.sleep(5)
  55. 55
  56. 56 finally:
  57. 57     # 关闭 WebDriver
  58. 58     driver.quit()
复制代码
代码主要分为五大块,第一块为导入selenium库驱动网页所需要的包和驱动chrome浏览器的chrome_driver;第二块为初始化ChromeDriver Service主要为控制如何打开浏览器;第三块为初始化 WebDriver,并传入 ChromeDriver Service;第五块为具体打开网页和处理网页按钮的方法,这里使用了XPATH方式寻找网页元素。
第二节

要想实现网页自动化登录最简单的方法是使用cookie方式登录,通过保存之前登录网页的cookie实现网页的自动化登录,而且还可以避免使用自动化工具打开浏览器无法保存登录信息的问题。
  1. 1 from selenium import webdriver
  2. 2 from selenium.webdriver.chrome.service import Service
  3. 3 from selenium.webdriver.chrome.options import Options
  4. 4 import os
  5. 5 import json
  6. 6 import time
  7. 7
  8. 8 # 配置 ChromeDriver 路径,替换为你的 ChromeDriver 路径,你也可以将chromedriver拖入文件根目录,使用'./chromedriver.exe'路径。
  9. 9 chrome_driver_path = 'E:/Git_repo/VSCode_tmp/autopython/chromedriver-win64/chromedriver.exe'  # 替换为你的 ChromeDriver 路径
  10. 10
  11. 11 # 初始化 ChromeDriver Service
  12. 12 service = Service(chrome_driver_path)
  13. 13 options = Options()
  14. 14 options.add_argument("--disable-blink-features=AutomationControlled")  # 使浏览器不显示自动化控制的信息
  15. 15
  16. 16 # 初始化 WebDriver,并传入 ChromeDriver Service
  17. 17 driver = webdriver.Chrome(service=service, options=options)
  18. 18
  19. 19 try:
  20. 20     # 打开小红书首页
  21. 21     url = "https://www.xiaohongshu.com"
  22. 22     driver.get(url)
  23. 23     time.sleep(40)  # 等待页面加载完成
  24. 24
  25. 25     # 获取 cookies
  26. 26     cookies = driver.get_cookies()
  27. 27
  28. 28     # 打印当前工作目录
  29. 29     print("Current working directory: ", os.getcwd())
  30. 30
  31. 31
  32. 32     # 将 cookies 保存到文件
  33. 33     with open("cookies.json", "w") as f:
  34. 34         json.dump(cookies, f)
  35. 35     print("Cookies have been saved to cookies.json")
  36. 36 finally:
  37. 37     # 关闭 WebDriver
  38. 38     driver.quit()
复制代码
上面程序通过手动登录网页而后再保存网页cookie用于之后的自动化登录。
第三节

通过上面的保存的cookie,编写程序实现网页自动登录并实现指定内容搜索。
  1.   1 from selenium import webdriver
  2.   2 from selenium.webdriver.chrome.service import Service
  3.   3 from selenium.webdriver.chrome.options import Options
  4.   4 from selenium.webdriver.common.by import By
  5.   5 from selenium.webdriver.support.ui import WebDriverWait
  6.   6 from selenium.webdriver.support import expected_conditions as EC
  7.   7
  8.   8 from selenium.common.exceptions import NoSuchElementException
  9.   9 import random
  10. 10 import os
  11. 11 import time
  12. 12 import json
  13. 13
  14. 14
  15. 15 from selenium.webdriver.common.keys import Keys
  16. 16
  17. 17 # 配置 ChromeDriver 路径,替换为你的 ChromeDriver 路径,你也可以将chromedriver拖入文件根目录,使用'./chromedriver.exe'路径。
  18. 18 chrome_driver_path = 'E:/Git_repo/VSCode_tmp/autopython/chromedriver-win64/chromedriver.exe'  # 替换为你的 ChromeDriver 路径
  19. 19
  20. 20 # 初始化 ChromeDriver Service
  21. 21 service = Service(chrome_driver_path)
  22. 22 # 打开浏览器时的相关配置,可以根据需求进行打开和关闭
  23. 23 options = Options()
  24. 24 options.add_argument("--start-maximized")  # 启动时最大化窗口
  25. 25 options.add_argument("--disable-blink-features=AutomationControlled")  # 使浏览器不显示自动化控制的信息
  26. 26 # options.add_argument("--disable-gpu")  # 禁用GPU硬件加速
  27. 27 options.add_argument("--disable-infobars")  # 隐藏信息栏
  28. 28 # options.add_argument("--disable-extensions")  # 禁用所有扩展程序
  29. 29 # options.add_argument("--disable-popup-blocking")  # 禁用弹出窗口拦截
  30. 30 # options.add_argument("--incognito")  # 启动无痕模式
  31. 31 # options.add_argument("--no-sandbox")  # 关闭沙盒模式(提高性能)
  32. 32 # options.add_argument("--disable-dev-shm-usage")  # 使用/dev/shm分区以避免共享内存问题
  33. 33 # options.add_argument("--remote-debugging-port=9222")  # 启用远程调试端口
  34. 34 options.add_experimental_option("excludeSwitches", ["enable-automation"])
  35. 35 options.add_experimental_option("useAutomationExtension", False)
  36. 36
  37. 37
  38. 38 MAX_ITEMS_BEFORE_WRITING = 5  # 每收集 5 条数据就写入一次文件
  39. 39
  40. 40 def write_to_file(collected_items):
  41. 41     print("当前工作目录:", os.getcwd())
  42. 42     # 追加写入文件的逻辑
  43. 43     with open('result.json', 'a', encoding='utf-8') as file:
  44. 44         json_data = [json.dumps(item, ensure_ascii=False) for item in collected_items]
  45. 45         file.write('\n'.join(json_data) + '\n')
  46. 46     print("Results saved successfully.")
  47. 47
  48. 48 #随机延时函数,用来模拟动作比较快的点击操作
  49. 49 def random_delay(time_start, time_end):
  50. 50     delay = random.uniform(time_start, time_end)
  51. 51     time.sleep(delay)
  52. 52
  53. 53 def scroll_and_collect(driver, num_items):
  54. 54     collected_items = []
  55. 55     collected_count = 0
  56. 56     result_count = 0
  57. 57     while result_count < num_items:
  58. 58         # 获取了页面上的所有选项卡
  59. 59         items = driver.find_elements(By.XPATH, "//section[@class='note-item']")
  60. 60         # 遍历获取的列表,分析里面的元素
  61. 61         for item in items:
  62. 62             try:
  63. 63                 # 找到元素里封面、标题、作者昵称、作者头像等元素
  64. 64                 cover = item.find_element(By.XPATH, ".//a[@class='cover mask ld']/img").get_attribute("src")
  65. 65                 title = item.find_element(By.XPATH, ".//a[@class='title']//span").text
  66. 66                 author_avatar = item.find_element(By.XPATH, ".//a[@class='author']//img[@class='author-avatar']").get_attribute("src")
  67. 67                 author_name = item.find_element(By.XPATH, ".//a[@class='author']//span").text
  68. 68
  69. 69                 # 存储获取的结果
  70. 70                 collected_items.append({
  71. 71                     "cover": cover,
  72. 72                     "title": title,
  73. 73                     "author_avatar": author_avatar,
  74. 74                     "author_name": author_name
  75. 75                 })
  76. 76                 result_count += 1
  77. 77                 collected_count += 1
  78. 78                 # 写入文件
  79. 79                 if collected_count >= MAX_ITEMS_BEFORE_WRITING:
  80. 80                     write_to_file(collected_items)
  81. 81                     collected_items = []  # 清空已收集的项
  82. 82                     collected_count = 0  # 重置计数器
  83. 83             except NoSuchElementException:
  84. 84                 continue
  85. 85         # 翻页
  86. 86         driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  87. 87         random_delay(4, 8)  # 等待页面加载新的内容
  88. 88
  89. 89         # 等待新内容加载的逻辑
  90. 90         try:
  91. 91             WebDriverWait(driver, 10).until(
  92. 92                 EC.presence_of_element_located((By.XPATH, "//section[@class='note-item']"))
  93. 93             )
  94. 94         except NoSuchElementException:
  95. 95             break
  96. 96         # 最后一次写入剩余的项
  97. 97     if collected_count > 0:
  98. 98         write_to_file(collected_items)
  99. 99     return collected_items
  100. 100
  101. 101 # 初始化 WebDriver,并传入 ChromeDriver Service
  102. 102 driver = webdriver.Chrome(service=service, options=options)
  103. 103 # # 防反爬:隐藏 webdriver 特征
  104. 104 # driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => false});")
  105. 105
  106. 106 try:
  107. 107     # 打开小红书首页
  108. 108     url = "https://www.xiaohongshu.com"
  109. 109     driver.get(url)
  110. 110
  111. 111     #time.sleep(4)  # 等待页面加载完成,后面改成显式等待
  112. 112     # 显式等待,直到二维码登录出现
  113. 113     wait = WebDriverWait(driver, 5)
  114. 114     try:
  115. 115         # 通过Xpath表达式获取到XHS登录框的html元素,判断是否可以登录
  116. 116         ## 此处需要重新确认XPATH元素,修改不使用直接复制F12值
  117. 117         search_box = wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='app']/div[1]/div/div[1]/div[2]")))
  118. 118         print("成功等待登录界面")
  119. 119     except Exception as e:
  120. 120         print(f"等待登录界面超时,错误信息:{e}")
  121. 121
  122. 122     #读取之前的cookies
  123. 123     with open("cookies.json", "r") as f:
  124. 124         cookies = json.load(f)
  125. 125     # 添加每个cookie到浏览器
  126. 126     for cookie in cookies:
  127. 127         driver.add_cookie(cookie)
  128. 128     
  129. 129     #刷新页面
  130. 130     driver.refresh()
  131. 131
  132. 132     wait = WebDriverWait(driver, 3)
  133. 133     # 等待登录按钮 **消失**(最多 3 秒),也可以判断cookie是否有效
  134. 134     try:
  135. 135         wait.until(EC.invisibility_of_element_located((By.XPATH, '//*[@id="login-btn"]')))
  136. 136         print("成功登录小红书")
  137. 137     except Exception as e:
  138. 138         print(f"登录小红书失败,错误信息:{e}")
  139. 139
  140. 140     time.sleep(5)
  141. 141      # 显式等待,直到搜索框出现
  142. 142     wait = WebDriverWait(driver, 10)
  143. 143     search_box = wait.until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='搜索小红书']")))
  144. 144     search_box.send_keys("漫展")
  145. 145     time.sleep(5)
  146. 146     search_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='input-button']")))
  147. 147     search_button.click()
  148. 148     time.sleep(5)
  149. 149     wait.until(EC.presence_of_element_located((By.XPATH, "//section[@class='note-item']")))
  150. 150     time.sleep(5)
  151. 151
  152. 152     num_items = 100
  153. 153     scroll_and_collect(driver, num_items)
  154. 154
  155. 155     '''##单次测试代码
  156. 156
  157. 157     items = driver.find_elements(By.XPATH, "//section[@class='note-item']")
  158. 158     print(f"找到 {len(items)} 个笔记项")
  159. 159     print(items)
  160. 160     collected_items = []
  161. 161     collected_count = 0
  162. 162     result_count = 0
  163. 163     for item in items:
  164. 164         try:
  165. 165             # 找到元素里封面、标题、作者昵称、作者头像等元素
  166. 166             cover = item.find_element(By.XPATH, ".//a[@class='cover mask ld']/img").get_attribute("src")
  167. 167             title = item.find_element(By.XPATH, ".//a[@class='title']//span").text
  168. 168             author_avatar = item.find_element(By.XPATH, ".//a[@class='author']//img[@class='author-avatar']").get_attribute("src")
  169. 169             author_name = item.find_element(By.XPATH, ".//a[@class='author']//span").text
  170. 170
  171. 171             # 存储获取的结果
  172. 172             collected_items.append({
  173. 173                 "cover": cover,
  174. 174                 "title": title,
  175. 175                 "author_avatar": author_avatar,
  176. 176                 "author_name": author_name
  177. 177             })
  178. 178             result_count += 1
  179. 179             collected_count += 1
  180. 180             # 写入文件
  181. 181             if collected_count >= MAX_ITEMS_BEFORE_WRITING:
  182. 182                 write_to_file(collected_items)
  183. 183                 collected_items = []  # 清空已收集的项
  184. 184                 collected_count = 0  # 重置计数器
  185. 185         except NoSuchElementException:
  186. 186             continue
  187. 187         
  188. 188         '''
  189. 189     # 延时几秒以便查看搜索结果
  190. 190     time.sleep(60)
  191. 191
  192. 192 finally:
  193. 193     # 关闭 WebDriver
  194. 194     driver.quit()
复制代码
构造登录环境和初始化WebDriver与第一节一致,这里不重复,这里主要讲代码打开网页之后的操作。整个执行部分分为两块,一块是自动登录,一块是网页指定内容搜索。
自动登录主要分为三步。第一步,进入网页后检测是否有登录二维码,如果有则认为网页加载完成可以进行登录;第二步,通过导入之前保存的cookie,刷新完成网页的自动登录;第三步,通过检测网页是否存在登录按钮,判断登录操作是否成功,如果没有则认为成功。
网页指定内容搜索主要是通过找到搜索框并将指定文字内容写入实现功能。根据网页的特点保存网页元素,并在每个元素内保存指定的字段内容用于其他。
 
总结

在完成这些程序之前我根本没有网页自动化的任何知识,通过阅读前人的教程和询问AI居然可以实现基本的功能,这让我再一次认识到AI能力的强大,至少在python自动化上已经超过大多数人了!!!
 
参考链接

Python使用selenium库实现网页自动化登陆以及数据抓取(爬虫)教程 - 云霄雨霁 - 博客园
30分钟上手Web自动化测试:Python+Selenium零基础实战指南 - xiaodi888 - 博客园
Selenium 教程 | 菜鸟教程
入门指南 | Selenium
通义AI助手
 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册