iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python实战快速上手BeautifulSoup库爬取专栏标题和地址
  • 226
分享到

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

2024-04-02 19:04:59 226人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录安装解析标签解析属性根据class值解析根据ID解析多层筛选提取a标签中的网址实战-获取博客专栏 标题+网址BeautifulSoup库快速上手 安装 pip install

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库内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

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

本文链接: https://www.lsjlt.com/news/154978.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作