python 爬虫之BeautifulS

发布时间:2019-06-30 16:51:51编辑:auto阅读(1843)

    import urllib2
    url = 'http://www.someserver.com/cgi-bin/register.cgi'
    values = {}
    values['name'] = 'Michael Foord'
    values['location'] = 'Northampton'
    values['language'] = 'Python'

    data = urllib.urlencode(values) #数据进行编码生成get方式的请求字段
    req = urllib2.Request(url,data) #作为data参数传递到Request对象中 POST方式访问
    response = urllib2.urlopen(req) 返回一个类文件对象
    the_page = response.read()
    soup = BeautifulSoup(the_page,"html.parser") 通过类文件the_page 创建beautifulsoup对象,soup的内容就是页面的源码内容
    soup.prettify() 格式化后soup内容
    构造好BeautifulSoup对象后,借助find()和find_all()这两个函数,可以通过标签的不同属性轻松地把繁多的html内容过滤为你所想要的
    url_name = line.get('href') 获取a标签的url信息
    Title = line.get_text().strip() 获取a标签的文本内容
    Beautiful Soup支持Python标准库中的HTML解析器
    BeautifulSoup(markup, “html.parser”)
    BeautifulSoup(markup, “lxml”)
    BeautifulSoup(markup, “html5lib”)
    Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象
    soup.p.attrs 获取标签p的属性信息
    find_all( name , attrs , recursive , text , **kwargs )

    find_all() 方法搜索当前tag的所有tag子节点,并判断是否符合过滤器的条件
    1.name 参数
    传字符串:soup.find_all('b') 查找文档中所有的<b>标签
    传正在表达式 import re for tag in soup.find_all(re.compile("^b")) 正则表达式的 match() 来匹配内容
    传列表 soup.find_all(["a", "b"])
    传True for tag in soup.find_all(True) 查找到所有的tag
    传方法
    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('p'))
    2.keyword 参数
    soup.find_all(id='link2')
    soup.find_all(href=re.compile("elsie"))
    soup.find_all(href=re.compile("elsie"), id='link1')
    soup.findall("a", class="sister") 用 class 过滤, class 是 python 的关键词,加个下划线就可以
    data_soup.find_all(attrs={"data-foo": "value"}) 特殊属性用attrs 组成字典进行查询

    3.text 参数
    soup.find_all(text="Elsie")
    soup.find_all(text=["Tillie", "Elsie", "Lacie"])
    soup.find_all(text=re.compile("Dormouse"))
    4.limit 参数
    soup.find_all("a", limit=2)
    5.recursive 参数
    soup.html.find_all("title", recursive=False)

    find() 与find_all()的区别是,find()直接返回结果
    find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容
    find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点
    find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点
    find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点
    find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点

    CSS选择器
    1.通过标签名查找
    print soup.select('title')
    print soup.select('a')
    2.通过类名查找
    print soup.select('.sister')
    3.通过 id 名查找
    print soup.select('#link1')
    4.组合查找
    print soup.select('p #link1')
    5.属性查找
    print soup.select('a[class="sister"]')
    print soup.select('a[href="http://example.com/elsie"]')
    print soup.select('p a[href="http://example.com/elsie"]')
    select 方法返回的结果都是列表形式,可以遍历形式输出,然后用 get_text() 方法来获取它的内容
    soup.a.attrs) # 获取a标签的所有属性(注意到格式是字典)

关键字