gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python实战快速上手BeautifulSoup库爬取专栏标题和地址由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

BeautifulSoup库快速上手 。

  。

安装

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

使用PyCharm的命令行 。

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

  。

解析标签

from bs4 import BeautifulSoupimport requestsurl="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).texts=BeautifulSoup(html,"html.parser")title =s.select("h2")for i in title:    print(i.text)

第一行代码:导入BeautifulSoup库 。

第二行代码:导入requests 。

第3、4、五行代码:获取url的html 。

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

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

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

  。

解析属性

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

  。

根据class值解析

from bs4 import BeautifulSoupimport requestsurl="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).texts=BeautifulSoup(html,"html.parser")title =s.select(".column_article_title")for i in title:    print(i.text)

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

  。

根据ID解析

from bs4 import BeautifulSouphtml="""<div class="crop-img-before">         <img src=""      </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)

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

  。

多层筛选

from bs4 import BeautifulSouphtml="""<div class="crop-img-before">         <img src=""      </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"])

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

  。

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

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

from bs4 import BeautifulSoupimport requestsimport reurl="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).texts=BeautifulSoup(html,"html.parser")title =s.select(".column_article_list li a")for i in title:    print((re.findall("原创.*?(.*?)",i.text))[0].lstrip())    print(i["href"])

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

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

原文链接:https://blog.csdn.net/weixin_42403632/article/details/120852131 。

最后此篇关于Python实战快速上手BeautifulSoup库爬取专栏标题和地址的文章就讲到这里了,如果你想了解更多关于Python实战快速上手BeautifulSoup库爬取专栏标题和地址的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com