首页 > 其他 > 详细

BeautifulSoup4----利用find_all和get方法来获取信息

时间:2017-02-12 10:45:33      阅读:790      评论:0      收藏:0      [点我收藏+]
  •  官方教学网页源码:

<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

  • 还有一种思想是按照标签,一层一层的查找信息,查看例子

BeautifulSoup4----利用find_all和get方法来获取信息

原文:http://www.cnblogs.com/gopythoner/p/6390381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!