日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/21 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫篇——selenium(webdriver)进行用户登录并爬取数据) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

爬蟲篇——selenium(webdriver)進(jìn)行用戶登錄并爬取數(shù)據(jù)

  • 摘要
  • (一)創(chuàng)建browser對(duì)象
  • (二)用戶登錄
  • (三)數(shù)據(jù)爬取

摘要

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

(一)創(chuàng)建browser對(duì)象

chromedriver.exe的下載地址為:點(diǎn)此進(jìn)行下載

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, '//*****')))# 根據(jù)自己的網(wǎng)頁進(jìn)行設(shè)置ActionChains(self.browser).click(button).perform()self.wait.until(expected_conditions.presence_of_element_located((by.CLASS_NAME, '******')))

(三)數(shù)據(jù)爬取

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

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

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。