日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

爬虫篇——selenium(webdriver)进行用户登录并爬取数据)

發布時間:2025/3/21 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫篇——selenium(webdriver)进行用户登录并爬取数据) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

爬蟲篇——selenium(webdriver)進行用戶登錄并爬取數據

  • 摘要
  • (一)創建browser對象
  • (二)用戶登錄
  • (三)數據爬取

摘要

本文主要介紹了如何通過selenium使用Chormedriver進行用戶登錄并爬取數據,使用過程中需注意合理使用selenium.webdriver.support.expected_conditions
selenium.webdriver.support.ui.WebDriverWait

(一)創建browser對象

chromedriver.exe的下載地址為:點此進行下載

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions from selenium.webdriver.common import by from selenium.webdriver.common.action_chains import ActionChains from lxml import etreeclass ChromeCrawl(object):def __init__(self):chrome_options = webdriver.ChromeOptions()prefs = {"profile.managed_default_content_settings.images": 2}chrome_options.add_experimental_option("prefs", prefs)chrome_options.add_experimental_option('excludeSwitches', ['enable-automation']) chrome_options.add_argument('--headless')chrome_options.add_argument('--no-sandbox')chrome_options.add_argument('--disable-gpu')self.browser = webdriver.Chrome(executable_path="./tools/chromedriver.exe", chrome_options=chrome_options)self.browser.set_page_load_timeout(60)self.browser.set_script_timeout(60)self.wait = WebDriverWait(self.browser, 60)

(二)用戶登錄

def login(self):username = "*****"passwd = "******"self.browser.get('https:********login')self.browser.implicitly_wait(60)elem = self.browser.find_element_by_id("username")elem.send_keys(username)elem = self.browser.find_element_by_id("password")elem.send_keys(passwd)button = self.wait.until(expected_conditions.element_to_be_clickable((by.XPATH, '//*****')))# 根據自己的網頁進行設置ActionChains(self.browser).click(button).perform()self.wait.until(expected_conditions.presence_of_element_located((by.CLASS_NAME, '******')))

(三)數據爬取

def crawl(self):self.browser.get('https:******')self.wait.until(expected_conditions.presence_of_element_located((by.CLASS_NAME, '******')))html = etree.HTML(self.browser.page_source)tmp = html.xpath('//*****')

by CyrusMay 2022 01 25

一生要有多少的輾轉
才能走到幸福的彼岸
——————五月天(青空未來)——————

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的爬虫篇——selenium(webdriver)进行用户登录并爬取数据)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。