當前位置:
首頁 >
爬虫篇——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
一生要有多少的輾轉
才能走到幸福的彼岸
——————五月天(青空未來)——————
總結
以上是生活随笔為你收集整理的爬虫篇——selenium(webdriver)进行用户登录并爬取数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 异步编程——asyncio
- 下一篇: 强化学习(二)—— 价值学习(Value