爬虫-request以及beautisoup模块笔记

requests模块

pip3 install requests

res = requests.get('')

res.text

res.cookies.get_dict()

res.content

res.encoding

res.aparent_encoding

res.status_code


res = requests.post(
    headers={
        'User-Agent': ''
    }
    url="",
    cookies={},
    data={}
)

request.request参数详解

1. method: 提交方式
2. url: 提交地址
3. params: 在 url 上传递的参数,以 get 形式的提交数据
4. data:  在 body 中提交的数据,字典:{"k1":'v1'}、字节:"k1=v1&k2=v2"、文件对象
5. json:  在 body 中提交的数据,把数据进行一个 json.dump()后一个字符串后发送,字典中嵌套字典时使用
6. headers: 请求头
        Referer:你上一次访问的地址
        User-Agent : 客户端
7. cookies: cookies置于 header 中发送
8. files:  文件对象
9. auth:  在 hrader 中加入加密的用户名和密码来做认证
10. timeout:  超时时间   (connect timeout, read timeout) <timeouts>.
11. allow_redirects: 是否允许重定向
12. proxies: 代理
        proxys={
            'http':'http://192.168.112:8080'
        }
13. verify: 布尔值,是否忽略https证书
14. stream: 流式处理,布尔值,从 response.iter_content()中迭代去取
15. cert: https 证书

request.session

s8.py测试,保存客户端历史访问信息

beautisoup 模块

1. name 根据标签名称获取标签

tag = soup.find('a')  # 查找
name = tag.name  # 获取a
tag.name = 'span' # 设置

2. attrs 标签的属性

# tag = soup.find('a')
# attrs = tag.attrs    # 获取
# print(attrs)
# tag.attrs = {'ik':123} # 设置会覆盖原来的所有属性
# tag.attrs['id'] = 'iiiii' # 增加新的属性
del tag.attrs['id']  删除某个属性
# print(soup)

3. children 所有儿子标签,第一层

tag.children

4. descendants 子子孙孙的标签

tag.descendants

5.删除

  • clear,将标签的所有儿子标签全部清空(保留自己标签)
  • decompose 递归的删除所有,包括自己
  • extract 递归的删除所有标签,并返回删除的,通 pop 方法

6. 字节与字符串之间的转换

  • decode 对象转换为字符串,含有当前标签
  • decode_contents 转换为字符串,不含有当前标签
  • encode 对象转换为字节、含有当前标签
  • encode_contents 转换为字节、不含有当前标签

7. 匹配

  • find
tag = soup.find(
    name='a',  # 标签
    attrs={'class': 'sister'},  # 属性
    recursive=True,  # 布尔值、True(只匹配一层,默认值);False(递归寻找)
    text='Lacie'  # 标签文本匹配
)
tags = soup.find(name='a', class_='sister', recursive=True, text='Lacie')

  • find_all
tags = soup.find_all(
    'a',limit=1 # limit 只匹配一个
)

--------------------
v = soup.find_all(name=['a','div']) 匹配两个标签,关系为或
v = soup.find_all(class_=['sister0', 'sister']) 匹配多个属性,关系为或
v = soup.find_all(id=['link1','link2'])
= soup.find_all(href=['link1','link2'])

8. 自定义正则

import re
rep = re.compile('^h')
v = soup.find_all(name=rep)


rep = re.compile('sister.*')
v = soup.find_all(class_=rep)


rep = re.compile('http://www.oldboy.com/static/.*')
v = soup.find_all(href=rep)

9. 自定义方法帅选

def func(tag):
    return tag.has_attr('class') and tag.has_attr('id')

v = soup.find_all(name=func)

10. has_attr 是否有属性检测

11. text get_text 文本获取

12. index 获取标签在在某个标签中的索引位置

tag = soup.find('body')
v = tag.index(tag.find('div'))

tag = soup.find('body')
for i,v in enumerate(tag):
print(i,v)

13. is_empty_element 判断是否是空标签或者自闭合标签

判断是否为:'br' , 'hr', 'input', 'img', 'meta','spacer', 'link', 'frame', 'base'

tag = soup.find('br')
v = tag.is_empty_element

14. 获取当前的关联标签(属性)

soup.next  # 下一个
soup.next_element
soup.next_elements
soup.next_sibling
soup.next_siblings


tag.previous  # 上一个
tag.previous_element
tag.previous_elements
tag.previous_sibling
tag.previous_siblings


tag.parent  # 父标签
tag.parents  # 层级父标签

15. 获取当前的关联标签(方法)

tag.find_next(...)
tag.find_all_next(...)
tag.find_next_sibling(...)
tag.find_next_siblings(...)

tag.find_previous(...)
tag.find_all_previous(...)
tag.find_previous_sibling(...)
tag.find_previous_siblings(...)

tag.find_parent(...)
tag.find_parents(...)

参数同find_all

16. css 选择器,同 js 标签选择器使用

soup.select("title")

soup.select("p nth-of-type(3)")

soup.select("body a")

soup.select("html head title")

tag = soup.select("span,a")

soup.select("head > title")

soup.select("p > a")

soup.select("p > a:nth-of-type(2)")

soup.select("p > #link1")

soup.select("body > a")

soup.select("#link1 ~ .sister")

soup.select("#link1 + .sister")

soup.select(".sister")

soup.select("[class~=sister]")

soup.select("#link1")

soup.select("a#link2")

soup.select('a[href]')

soup.select('a[href="http://example.com/elsie"]')

soup.select('a[href^="http://example.com/"]')

soup.select('a[href$="tillie"]')

soup.select('a[href*=".com/el"]')


from bs4.element import Tag

def default_candidate_generator(tag):
    for child in tag.descendants:
        if not isinstance(child, Tag):
            continue
        if not child.has_attr('href'):
            continue
        yield child

tags = soup.find('body').select("a", _candidate_generator=default_candidate_generator)
print(type(tags), tags)

from bs4.element import Tag
def default_candidate_generator(tag):
    for child in tag.descendants:
        if not isinstance(child, Tag):
            continue
        if not child.has_attr('href'):
            continue
        yield child

tags = soup.find('body').select("a", _candidate_generator=default_candidate_generator, limit=1)
print(type(tags), tags)

17. 标签内容

tag = soup.find('span')
print(tag.string)          # 获取
tag.string = 'new content' # 设置
print(soup)

18. append 在当前标签内部最后追加一个标签对象

tag = soup.find('body')
tag.append(soup.find('a'))
print(soup)

from bs4.element import Tag
obj = Tag(name='i',attrs={'id': 'it'})
obj.string = '我是一个新来的'
tag = soup.find('body')
tag.append(obj)
print(soup)

19. insert 在当前标签内部指定位置插入一个标签对象

from bs4.element import Tag
obj = Tag(name='i', attrs={'id': 'it'})
obj.string = '我是一个新来的'
tag = soup.find('body')
tag.insert(2, obj)
print(soup)

20. insert_after、insert_before 在指定标签的前面或者和面插入一个标签对象

21. replace_with 将当前标签替换为指定的标签一个标签对象

22. 创建标签之间的关系,不修改 html 中的位置,只修改逻辑属性关系

tag = soup.find('div')
a = soup.find('a')
tag.setup(previous_sibling=a)
print(tag.previous_sibling)

23. wrap 将指定标签把当前标签包裹起来

div = soup.find('div')
a = soup.find('a')
div.wrap(a) # 将 div 包裹进 a标签中

24.unwrap,去掉当前标签,将保留其包裹的标签

a = '<div>测试<a>test</a></div>'
tag = soup.find('div')
v = tag.unwrap()
v = '测试<a>test</a>'  # 把外层的标签去除,将保留其包裹的标签 
原文地址:https://www.cnblogs.com/forsaken627/p/8622844.html