Python实现获取网页信息并解析

 更新时间:2025年05月22日 10:50:58   作者:KevinQ  
这篇文章主要为大家详细介绍了如何使用Python实现获取网页信息并解析功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

Python爬虫用到的两个主要的库是:bs4和request,request用于发起请求,而bs4用于网页元素解析。

以阮一峰老师的博客为例,每周最喜欢的是科学爱好者周刊中的“言论”不分,以 科技爱好者周刊(第 253 期)为例,让我们来看看能不能将言论部分提取出来。

import requests  
from bs4 import BeautifulSoup  
  
url = "http://www.ruanyifeng.com/blog/2023/05/weekly-issue-253.html"  
response = requests.get(url)  
soup = BeautifulSoup(response.content, "html.parser")  
first_tag = soup.find("h2", string="言论")  
next_sibling = first_tag.find_next_sibling()  
content1 = ""  
while next_sibling.name != "h2":  
    content1 += str(next_sibling.get_text())  
    # content1 += str(next_sibling)  
    content1 += "\n\n"  
    next_sibling = next_sibling.find_next_sibling()  
print(content1)

执行结果:

用到的重要函数是查找某个tag,获取某个tag的下一个tag函数:

find与find_all

函数定义如下:

def find(self, name=None, attrs={}, recursive=True, text=None,  
         **kwargs):  
    """Look in the children of this PageElement and find the first  
    PageElement that matches the given criteria.  
    All find_* methods take a common set of arguments. See the online    documentation for detailed explanations.  
    :param name: A filter on tag name.    :param attrs: A dictionary of filters on attribute values.    :param recursive: If this is True, find() will perform a        recursive search of this PageElement's children. Otherwise,        only the direct children will be considered.    :param limit: Stop looking after finding this many results.    :kwargs: A dictionary of filters on attribute values.    :return: A PageElement.  
    :rtype: bs4.element.PageElement  
    """    
    r = None  
    l = self.find_all(name, attrs, recursive, text, 1, **kwargs)  
    if l:  
        r = l[0]  
    return r
def find_all(self, name=None, attrs={}, recursive=True, text=None,  
             limit=None, **kwargs):  
    """Look in the children of this PageElement and find all  
    PageElements that match the given criteria.  
    All find_* methods take a common set of arguments. See the online    documentation for detailed explanations.  
    :param name: A filter on tag name.    :param attrs: A dictionary of filters on attribute values.    :param recursive: If this is True, find_all() will perform a        recursive search of this PageElement's children. Otherwise,        only the direct children will be considered.    :param limit: Stop looking after finding this many results.    :kwargs: A dictionary of filters on attribute values.    :return: A ResultSet of PageElements.  
    :rtype: bs4.element.ResultSet  
    """    
    generator = self.descendants  
    if not recursive:  
        generator = self.children  
    return self._find_all(name, attrs, text, limit, generator, **kwargs)

find 返回的是一个元素,find_all返回的是一个列表,举例说明比较清晰。

允许传入的参数包括:

1.字符串:tag的名称,如h2, p, b, a等等分别表示查找<h2>, <p>, <b>, <a>等标签。 如:

soup.find_all('b')
# [<b>这里加粗</b>]

2.正则表达式

# 导入包
import re
for tag in soup.find_all(re.compile("^b")):
    print(tag.name)
# 结果会找出 body, b等b开头的标签

.3列表:与列表中任一元素匹配的内容返回

soup.find_all(["a", "b"])
# 输出: [<b>加粗</b>,
#  <a class="ddd" href="http://xxx" rel="external nofollow" >xxx</a> ]

4.True: 返回所有非字符串节点。

5.方法:传入的方法接受唯一参数:元素,并返回True或者False,若元素计算的值为True,则返回。

# 判断一个tag有class属性,但是没有id属性
def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
# 使用方式
soup.find_all(has_class_but_no_id)

6.对元素指定判断函数:

# 查找所有href标签不是https的a标签
def not_https(href):
        return href and not re.compile("https").search(href)
soup.find_all(href=not_https)

通过上述第5种和第6种方法,可以构造很复杂的tag过滤函数,从而实现过滤目的。

其他相关搜索函数如下:

find_next_sibling 返回后面的第一个同级tag节点 find_previous_sibling 返回前面的第一个同级tag节点 find_next 后面第一个tag节点 find_previous 前面第一个tag节点

更多内容可以在bs4官方文档中查看。

到此这篇关于Python实现获取网页信息并解析的文章就介绍到这了,更多相关Python获取网页信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python django model联合主键的例子

    python django model联合主键的例子

    今天小编就为大家分享一篇python django model联合主键的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python求出0~100以内的所有素数

    Python求出0~100以内的所有素数

    质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。下面小编给大家带来了Python求出0~100以内的所有素数实例代码,需要的朋友参考下
    2018-01-01
  • 浅谈Pytorch torch.optim优化器个性化的使用

    浅谈Pytorch torch.optim优化器个性化的使用

    今天小编就为大家分享一篇浅谈Pytorch torch.optim优化器个性化的使用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • Django-imagekit的使用详解

    Django-imagekit的使用详解

    ImageKit是一个用于处理图像的Django应用程序。这篇文章主要介绍了Django-imagekit的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • Python面向对象之内置函数相关知识总结

    Python面向对象之内置函数相关知识总结

    本次要总结的的内置函数共8个,他们都跟面向对象的知识相关,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • Python3实现定时任务的四种方式

    Python3实现定时任务的四种方式

    Python实现定点与定时任务方式比较多,找到下面四中实现方式,每个方式都有自己应用场景;下面来快速介绍Python中常用的定时任务实现方式,一起看看吧
    2019-06-06
  • 举例详解Python中threading模块的几个常用方法

    举例详解Python中threading模块的几个常用方法

    这篇文章主要介绍了举例详解Python中threading模块的几个常用方法,threading模块用来创建和操作线程,是Python学习当中的重要知识,需要的朋友可以参考下
    2015-06-06
  • python并发编程多进程 互斥锁原理解析

    python并发编程多进程 互斥锁原理解析

    这篇文章主要介绍了python并发编程多进程 互斥锁原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 教你用pyecharts绘制各种图表案例(效果+代码)

    教你用pyecharts绘制各种图表案例(效果+代码)

    说到pyecharts,相信很多人不会陌生,一个优秀的python可视化包,下面这篇文章主要给大家介绍了关于如何用pyecharts绘制各种图表案例的相关资料,需要的朋友可以参考下
    2022-06-06
  • Python 自动化表单提交实例代码

    Python 自动化表单提交实例代码

    今天以一个表单的自动提交,来进一步学习selenium的用法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-06-06

最新评论