Python实战快速上手BeautifulSoup库爬取专栏标题和地址

 更新时间:2021年10月20日 14:48:50   作者:小旺不正经  
BeautifulSoup是爬虫必学的技能,BeautifulSoup最主要的功能是从网页抓取数据,Beautiful Soup自动将输入文档转换为Unicode编码,输出文档转换为utf-8编码

BeautifulSoup库快速上手

安装

pip install beautifulsoup4
# 上面的安装失败使用下面的 使用镜像
pip install beautifulsoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple

使用PyCharm的命令行

image-20211019110243706

解析标签

from bs4 import BeautifulSoup
import requests
url='https://blog.csdn.net/weixin_42403632/category_11076268.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('h2')
for i in title:
    print(i.text)

第一行代码:导入BeautifulSoup库

第二行代码:导入requests

第三、四、五行代码:获取url的html

第六行代码:激活BeautifulSoup库 'html.parser'设置解析器为HTML解析器

第七行代码:选取所有<h2>标签

image-20211019142518434

解析属性

BeautifulSoup库 支持根据特定属性解析网页元素

根据class值解析

from bs4 import BeautifulSoup
import requests
url='https://blog.csdn.net/weixin_42403632/category_11076268.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('.column_article_title')
for i in title:
    print(i.text)

image-20211019142955305

根据ID解析

from bs4 import BeautifulSoup
html='''<div class="crop-img-before">
         <img src="" alt="" id="cropImg">
      </div>
        <div id='title'>
        测试成功
        </div>
      <div class="crop-zoom">
         <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="bt-reduce">-</a><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="bt-add">+</a>
      </div>
      <div class="crop-img-after">
         <div  class="final-img"></div>
      </div>'''
s=BeautifulSoup(html,'html.parser')
title =s.select('#title')
for i in title:
    print(i.text)

image-20211019143400421

多层筛选

from bs4 import BeautifulSoup
html='''<div class="crop-img-before">
         <img src="" alt="" id="cropImg">
      </div>
        <div id='title'>
        456456465
        <h1>测试成功</h1>
        </div>
      <div class="crop-zoom">
         <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="bt-reduce">-</a><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="bt-add">+</a>
      </div>
      <div class="crop-img-after">
         <div  class="final-img"></div>
      </div>'''
s=BeautifulSoup(html,'html.parser')
title =s.select('#title')
for i in title:
    print(i.text)
title =s.select('#title h1')
for i in title:
    print(i.text)

提取a标签中的网址

title =s.select('a')
for i in title:
    print(i['href'])

image-20211019144002419

实战-获取博客专栏 标题+网址

image-20211019184236143

from bs4 import BeautifulSoup
import requests
import re
url='https://blog.csdn.net/weixin_42403632/category_11298953.html'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0'}
html=requests.get(url,headers=headers).text
s=BeautifulSoup(html,'html.parser')
title =s.select('.column_article_list li a')
for i in title:
    print((re.findall('原创.*?\n(.*?)\n',i.text))[0].lstrip())
    print(i['href'])

image-20211019184252204

到此这篇关于Python实战快速上手BeautifulSoup库爬取专栏标题和地址的文章就介绍到这了,更多相关Python BeautifulSoup库内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python中的日志文件按天分割

    python中的日志文件按天分割

    这篇文章主要介绍了python中的日志文件按天分割方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • 一文教你用python编写Dijkstra算法进行机器人路径规划

    一文教你用python编写Dijkstra算法进行机器人路径规划

    迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径,这篇文章主要给大家介绍了关于利用python编写Dijkstra算法进行机器人路径规划的相关资料,需要的朋友可以参考下
    2021-08-08
  • Python数据处理篇之Sympy系列(五)---解方程

    Python数据处理篇之Sympy系列(五)---解方程

    这篇文章主要介绍了Python数据处理篇之Sympy系列(五)---解方程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • Python3中数据校验机制详解

    Python3中数据校验机制详解

    在日常编码环节,很大比例的错误处理工作和参数的输入有关,所以这篇文章主要来和大家介绍一下Python3中的数据校验机制,感兴趣的可以了解下
    2024-04-04
  • 简单谈谈Python的pycurl模块

    简单谈谈Python的pycurl模块

    PycURl是一个C语言写的libcurl的python绑定库。libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库。它的功能很强大,PycURL 是一个非常快速(参考多并发操作)和丰富完整特性的,但是有点复杂的接口。
    2018-04-04
  • Python编程实现及时获取新邮件的方法示例

    Python编程实现及时获取新邮件的方法示例

    这篇文章主要介绍了Python编程实现及时获取新邮件的方法,涉及Python实时查询邮箱及邮件获取相关操作技巧,需要的朋友可以参考下
    2017-08-08
  • Python Pandas轻松实现数据清理

    Python Pandas轻松实现数据清理

    在当今的数据驱动时代,数据清理是数据分析、机器学习项目中至关重要的一步,本文将带大家轻松上手使用Python和Pandas进行数据清理,希望对大家有所帮助
    2024-12-12
  • Python实现移动指定图片到指定目录

    Python实现移动指定图片到指定目录

    这篇文章主要为大家详细介绍了如何使用Python的os和shutil库实现自动化查找和移动图片功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2025-02-02
  • python使用celery实现异步任务执行的例子

    python使用celery实现异步任务执行的例子

    今天小编就为大家分享一篇python使用celery实现异步任务执行的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python面向对象编程关键深度探索类与对象

    Python面向对象编程关键深度探索类与对象

    这篇文章主要为大家介绍了Python面向对象编程关键深度探索类与对象示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05

最新评论