zoukankan      html  css  js  c++  java
  • 01、博客爬虫

        你需要爬取的是博客【人人都是蜘蛛侠】中,《未来已来(四)——Python学习进阶图谱》的所有文章评论,并且打印。
     
     
     1 #1、博客爬虫
     2 #    你需要爬取的是博客【人人都是蜘蛛侠】中,《未来已来(四)——Python学习进阶图谱》的所有文章评论,并且打印。
     3 #    文章URL:https://wordpress-edu-3autumn.localprod.forc.work/all-about-the-future_04/
     4 import requests
     5 from bs4 import BeautifulSoup
     6 res = requests.get('https://wordpress-edu-3autumn.localprod.forc.work/all-about-the-future_04/')
     7 html = res.text
     8 soup = BeautifulSoup(html,'html.parser')
     9 items = soup.find_all('div',class_='comment-content')
    10 for item in items:
    11     print(item.find('p').text)
    12 
    13 '''
    14 执行结果如下:
    15     测试评论
    16     我们就是
    17     minu
    18     kpi
    19 '''
    20 
    21 '''
    22 #   下面是老师的代码
    23 
    24 #   调用requests库
    25 import requests
    26 #   调用BeautifulSoup库
    27 from bs4 import BeautifulSoup
    28 #   把网址复制给变量destnation_url
    29 url_destnation = 'https://wordpress-edu-3autumn.localprod.forc.work/all-about-the-future_04/'
    30 #   返回一个response对象,赋值给destnation
    31 res_comment = requests.get (url_destnation)
    32 #   把网页解析为BeautifulSoup对象
    33 bs_comment = BeautifulSoup(res_comment.text,'html.parser')
    34 #   通过匹配属性提取出我们想要的元素
    35 list_comments = bs_comment.find_all('div',class_= 'comment-content')
    36 #   遍历列表,取出列表中的每一个值
    37 for tag_comment in list_comments:
    38 #   打印评论的文本
    39     print(tag_comment.text)
    40 '''
     
    items中每个Tag的内容如下
    1 <div class="comment-content">
    2 <p>第1个蜘蛛侠</p>
    3 </div>
  • 相关阅读:
    java
    GO学习Day2
    GO学习Day1
    APS定时任务框架
    用微信每天给女朋友说晚安
    人生苦短,我用python
    Python 捕获terminate信号优雅关闭进程
    Python 多线程及多进程结合使用
    Python API 接口权限控制思路
    Docker runC 严重安全漏洞CVE-2019-5736 导致容器逃逸
  • 原文地址:https://www.cnblogs.com/www1707/p/10692298.html
Copyright © 2011-2022 走看看