爬虫篇——selenium(webdriver)进行用户登录并爬取数据)
生活随笔
收集整理的這篇文章主要介紹了
爬虫篇——selenium(webdriver)进行用户登录并爬取数据)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
爬蟲篇——selenium(webdriver)進(jìn)行用戶登錄并爬取數(shù)據(jù)
- 摘要
- (一)創(chuàng)建browser對(duì)象
- (二)用戶登錄
- (三)數(shù)據(jù)爬取
摘要
本文主要介紹了如何通過(guò)selenium使用Chormedriver進(jìn)行用戶登錄并爬取數(shù)據(jù),使用過(guò)程中需注意合理使用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)頁(yè)進(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)
才能走到幸福的彼岸
——————五月天(青空未來(lái))——————
總結(jié)
以上是生活随笔為你收集整理的爬虫篇——selenium(webdriver)进行用户登录并爬取数据)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 异步编程——asyncio
- 下一篇: 强化学习(一)——专业术语及OpenAI