zoukankan      html  css  js  c++  java
  • Python selenium糗事百科

    一个简单的爬虫入门代码,爬取糗事百科主页的段子(不包括图片,仅文字)

    1. 需要安装seleniumChromeDriver
    2. 将chromedriver.exe放在Chrome的安装目录下。
    3. 配置环境变量。点击我的电脑->属性->高级系统设置->PATH->新建(Chrome的安装位置,比如我的是:C:Program Files (x86)GoogleChromeApplication)
      #/usr/bin/env python
      #coding:utf-8
      
      #导入selenium
      from selenium import webdriver
      
      class Qiubai:
          def __init__(self):
              #打开Chrome浏览器
              self.dr = webdriver.Chrome()
              #访问糗事百科主页
              self.dr.get('https://www.qiushibaike.com/')
      
          def print_content(self):
              #获取id为“content-left”的元素
              main_content = self.dr.find_element_by_id('content-left')
              #获取class为“content”的元素
              contents = main_content.find_elements_by_class_name('content')
      
              #通过for循环输出获取到的内容
              i = 1
              for content in contents:
                  print(str(i) + "." + content.text +'
      ')
                  i += 1
      
              self.quit()
      
          def quit(self):
              #关闭浏览器
              self.dr.quit()
      
      Qiubai().print_content()
  • 相关阅读:
    redis--列表
    redis ——字符串
    redis 第一节 redis安装、PHP扩展 、主从
    Python--day7
    Python--day6
    Python爬虫
    JSON基础
    Python--day5
    Python—day3
    Windows10 安装QT问题
  • 原文地址:https://www.cnblogs.com/jdy113/p/8035506.html
Copyright © 2011-2022 走看看