我是靠谱客的博主 悲凉豆芽,最近开发中收集的这篇文章主要介绍BeautifulSoup4----利用find_all和get方法来获取信息中文文档,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

  • 中文文档

  •  官方教学网页源码:

<html>
<head>
<title>Page title</title>
</head>
<body>
<p id="firstpara" align="center">
This is paragraph<b>one</b>.
</p>
<p id="secondpara" align="blah">
This is paragraph<b>two</b>.
</p>
</body>
</html>
  • find方法的参数及意义

find(name=None, attrs={}, recursive=True, text=None, **kwargs)

1,按照tag(标签)搜索:

1 find(tagname)
# 直接搜索名为tagname的tag 如:find('head')
2 find(list)
# 搜索在list中的tag,如: find(['head', 'body'])
3 find(dict)
# 搜索在dict中的tag,如:find({'head':True, 'body':True})
4 find(re.compile('')) # 搜索符合正则的tag, 如:find(re.compile('^p')) 搜索以p开头的tag
5 find(lambda)
# 搜索函数返回结果为true的tag, 如:find(lambda name: if len(name) == 1) 搜索长度为1的tag
6 find(True)
# 搜索所有tag

  2,按照attrs(属性)搜索:

1 find('id'='xxx')
# 寻找id属性为xxx的
2 find(attrs={'id':re.compile('xxx'), 'algin':'xxx'}) # 寻找id属性符合正则且algin属性为xxx的
3 find(attrs={'id':True, 'algin':None})
# 寻找有id属性但是没有algin属性的

 

  • 利用BeautifulSoup4爬取豆瓣数据的ID

代码如下:

import requests
from bs4 import BeautifulSoup as bs
#以豆瓣‘编程’分类的一个连接URL为例子开始爬数据ID
url = 'https://book.douban.com/tag/编程?start=20&type=T'
res = requests.get(url)
#发送请求
#print(res.encoding)
#这个是用来查看网页编码的
#res.encoding = 'utf-8'
#跟上一个结合来用,如果编码有乱码,则可以通过这个定义编码来改变
html = res.text
#print(html)

IDs = []
soup
= bs(html,"html.parser")
#定义一个BeautifulSoup变量
items = soup.find_all('a',attrs={'class':'nbg'})
#print(items)
for i in items:
idl = i.get('href')
#print(idl)
id = idl.split('/')[4]
print(id)
IDs.append(id)
print('这一页收集到书籍ID数:%d' % len(IDs))

 

 

  • 第一部分是获取网页源代码的过程,使用requests模块
  • 第二部分为使用BeautifulSoup来解析网页,得到需要的信息
    • soup
      = bs(html,"html.parser")

      这句的意思是声明一个变量,用BeautifulSoup处理之后的原网页代码

    • items = soup.find_all('a',attrs={'class':'nbg'})

      这句的作用是查找a标签,当然,a标签会有很多,但是我们不需要所有,因此我们还需要判断一下这种a标签还有个属性是class='nbg',我们只需要这种a标签。items得到的是一个list

    • 属性都放着attrs这个字典中,当某个属性的值不是定值的时候,可以使用   '属性名':True  这种方式。
    • for i in items:
      idl = i.get('href')

      这句的意思是获取满足条件的每个a标签中属性‘href’的值

    • id = idl.split('/')[4]

      由于‘href’的属性是一个连接,但是我们只需要得到ID,所有可以将连接按照‘/’分解,提取ID

  • 具体的爬虫例子可以参照:智联招聘爬虫

  • Beautifulsoup的select选择器方法可以参考爬虫例子:前程无忧爬虫

转载于:https://www.cnblogs.com/gopythoner/p/6390381.html

最后

以上就是悲凉豆芽为你收集整理的BeautifulSoup4----利用find_all和get方法来获取信息中文文档的全部内容,希望文章能够帮你解决BeautifulSoup4----利用find_all和get方法来获取信息中文文档所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部