栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python+selenium浏览器模拟信息爬取

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python+selenium浏览器模拟信息爬取

python+selenium浏览器模拟信息爬取
      • 目录
          • 1 本篇重点
          • 2 目标需求
          • 3.python脚本

目录 1 本篇重点
  • selenium和bs4的结合使用
  • 解决【href="javascript:void(0)】问题

​ 在selenium进行浏览器模拟爬虫时,会遇见【href="javascript:void(0)】的标签内容,这里就无法进行点击行为,对于自己遇见的这一问题,如何处理是本篇的重点之一。


2 目标需求

​ 在不登录豆瓣网站的情况下,获取豆瓣电影主题模块中,最近热门电影第2页的《四海》

3.python脚本
"""
-*- coding: utf-8 -*-
-- @author: phil
-- @Time:2022/5/18 18:45
"""
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

class Demo_douban():
    def __init__(self):
        self.start_url = 'https://movie.douban.com/'
        self.driver = webdriver.Firefox()

    def SPT_one(self):
        # 进入电影主题模块
        self.driver.get(self.start_url)
        self.driver.find_element_by_xpath("//*[@class ='btn-next']").click()
        time.sleep(1)
        print('完成类似登录!')
        
    def wait(self, locator, timeout=5):
        '''等到元素加载完成'''
        WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))

    def Super_get(self):
        # 解决:href="javascript:void(0);" 的页面加载问题
        # 相对路径定位:find_element_by_xpath("//form[@id='form']/span/input")
        path_a = "//div[@class='gaia gaia-lite gaia-movie slide-mode']/div[@class='list-wp']/div/div[2]/a[@class='btn-next']"
        locator = (By.XPATH, path_a)
        self.wait(locator)
        # 异常的捕获
        try:
            elements = self.driver.find_elements_by_xpath(path_a)  # 再次获取元素,预防StaleElementReferenceException
            self.driver.execute_script('arguments[0].click();', elements[0])  # 模拟用户点击
            print('已经点击!')
        except NoSuchElementException:
            print('NoSuchElementException')
        time.sleep(3)
        print(self.driver.current_url)
        
    def bs4_execute(self):
        # bs4的html解析
        url_a = 'https://movie.douban.com/subject/35250237/?tag=热门&from=gaia'
        ele = self.driver.find_element_by_xpath("//a[@href = '{}' ]".format(url_a))
        html = ele.get_attribute('innerHTML')
        print('html是:' + html)
        time.sleep(1)
        soup = BeautifulSoup(html, 'html5lib')      # 指定用html5lib来解析文档
        # 在查找的html中再找子标签的内容
        target = soup.find('p').get_text().replace('n', '').split()       # target 是 str类型 .split('n')
        # 列表
        print('电影名称:' + 'n' + '{}'.format(target[0]))
        # self.driver.quit()

# 运行命令
if __name__ == '__main__':
    SPT = Demo_CSDNweb()
    SPT.SPT_one()
    SPT.Super_get()
    SPT.bs4_execute()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916017.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号