網站首頁 編程語言 正文
python+selenium編寫實現爬蟲過程:
- 1.爬蟲循環處理table表,
- 2.table表分頁處理,
- 3.網頁table所有內容循環處理
- 4.獲取隱藏的href超鏈接內容,
- 5.所有數據本地csv保存,
代碼如下:
from selenium.webdriver.chrome.options import Options import pandas as pd import math from selenium import webdriver import time
# 配置chrome的參數 options = Options() options.add_argument('--headless') # options.add_experimental_option('excludeSwitches', ['enable-automation']) #避開驗證碼 # 獲取一個瀏覽器對象 browser = webdriver.Chrome(chrome_options=options) browser.get("C:\\Users\\XXXX\\XXXX.html") # 打印整個表格信息 # print(browser.find_element_by_xpath("http://*[@id=\"versions-bom-layout\"]/div/div[5]/div/div/div/div[1]/table/tbody").text) ? ? data = [] ?# 建立空列表存儲表格信息 # 獲取總頁數(通過二次定位方法進行定位) # total_pages1 = len(browser.find_element_by_class_name("pagination").find_elements_by_tag_name("li")) - 2 # print("total_pages is %s" % (total_pages1)) ? # 共有多少個組件需要提取 total_num = \ ? ? browser.find_element_by_xpath("http://*[@id=\"versions-bom-layout\"]/div/div[5]/div/div/div/div[1]/div").text.split( ? ? ? ? '共')[1] print(f'total_num====::::::::{total_num}') ? total_pages = math.ceil(int(total_num) / 100) + 1 ?# 每頁顯示100個組件, 獲取總頁數 print(f'total_pages====::::::::{total_pages}') time.sleep(10) ?# ? # total_pages = len(browser.find_element_by_tag_name('select').find_element_by_tag_name('option')) for i in range(1, total_pages): ?# page:總共有total_pages-1頁 ? ? # # 獲取分頁輸入框標簽的定位 ? ? # inputpage = browser.find_element_by_xpath("http://*[@id='pager_center']/table/tbody/tr/td[5]/input") ? ? # # 首先清除輸入框里面的數字 ? ? # inputpage.clear() ? ? # # 然后發送頁碼,我們為了方便,就把第一頁也寫在了里面 ? ? # inputpage.send_keys(str(i)) ? ? # # 最后模擬鍵盤點擊Enter鍵 ? ? # inputpage.send_keys(Keys.ENTER) ? ? # # 為了是數據刷新出來,做了一個小小的死延時處理 ? ? ? time.sleep(10) ?# 讓瀏覽器先加載一下動態頁面 ? ? t_body = browser.find_element_by_tag_name('tbody') ?# 定位表格主體 ? ? tr_list = t_body.find_elements_by_tag_name('tr') ?# 定位表格每一行 ? ? numbers = len(tr_list) ? ? print(f'tr_list[0].text===={tr_list[0].text}') ? ? ? # for i in range(numbers): ? ? for tr in tr_list: ? ? ? ? td_list = tr.find_elements_by_tag_name('td') ?# 定位表格每個單元格 ? ? ? ? lst = [] ?# 建立空列表存儲每行信息 ? ? ? ? for td in td_list: ? ? ? ? ? ? # lst.append("B-momv2core-121-anyreport-BinaryScan") ?# 添加版本信息 ? ? ? ? ? ? lst.append(td.text) ?# 添加每個單元格的文本信息 ? ? ? ? ? ? href = td_list[4].find_element_by_tag_name('a').get_attribute("href") ?# 獲當前行第5個td單元格的超鏈接 ? ? ? ? print(u"第n行第n列的text:", href) ? ? ? ? lst.append(href) ?# print(u"第一行第二列的text:", href) ?# 添加文件鏈接 ? ? ? ? data.append(lst) ?# 添加每行信息 ? ? ? # 點擊下一頁邏輯判斷 ? ? # 如果table只有1頁,不作處理,直接退出循環 ? ? # 如果table只有2頁,點擊下一頁處理條件(total_pages == 3 and i == 1) ? ? # 如果table2頁以上,點擊下一頁處理條件(total_pages > 3 and i < (total_pages - 1)) ? ? if total_pages == 3 and i == 1: ? ? ? ? browser.find_element_by_xpath("http://*[contains(text(),'下一頁')]").click() ? ? ? elif total_pages > 3 and i < (total_pages - 1): ? ? ? ? browser.find_element_by_xpath("http://*[contains(text(),'下一頁')]").click() ? df = pd.DataFrame(data, ? ? ? ? ? ? ? ? ? columns=['選擇', '策略', '審核', '版本組', '匹配數', '3', '4', '5', '6', '7', '8', '9', '10', '來源鏈接']) df.to_csv('獲取未知xxx.csv', encoding='utf_8_sig') browser.quit()
效果圖:
原文鏈接:https://blog.csdn.net/baidu_31295661/article/details/122512862
相關推薦
- 2023-02-03 c語言統計素數之和的實例_C 語言
- 2022-04-11 MVVMLight項目Model?View結構及全局視圖模型注入器_Android
- 2023-04-02 C語言中的線程信號控制詳解_C 語言
- 2022-03-30 C語言代碼實現猜數字游戲_C 語言
- 2022-05-01 Python的裝飾器詳情介紹_python
- 2022-12-07 R語言隨機抽樣詳解_R語言
- 2022-09-23 Pandas中Apply函數加速百倍的技巧分享_python
- 2022-12-15 C++?Boost?Lambda表達式詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支