1.requests

1.method

  提交方式:post、get、put、delete、options、head、patch

2.url

  访问地址

3.params

  在url中传递的参数,GET

  params = {'k1':'v1','k2':'v2'}    params = ‘k1=v1&k2=v2’    params = [('k1','v1'),('k2,'v2')]

4.data

  在请求体内传递的参数

  data = {'k1':'v1','k2':'v2'}    data = ‘k1=v1&k2=v2’    data = [('k1','v1'),('k2,'v2')]    data = open('file','rb')

5.json

  在请求体内传递的参数

  JSON serializable Python object

  参数经过序列化,意味着可以传递字典内嵌套字典等

6.headers

  请求头

  headers = { 'referer':上次浏览的页面

        'user-agent':用户使用的客户端类型

        ...

          }

  

7.cookies

  即cookie

  字典类型或CookieJar object类型,在请求头中传递

8.files

  文件

  files = {'file1':open('file','rb')}    files = ('file1',open('file','rb'))     ===    ('filename', fileobj, 'content_type')  或  ('filename', fileobj, 'content_type', custom_headers)

9.auth

  用户名、密码加密    auth = HTTPBasicAuto(username,pwd)

10.timeout

  请求和响应的超时

11.allow_redirects

  是否允许重定向

.proxies

  代理

13.verify

  是否忽略证书

14.stream

  下载方式  类型为布尔值   True,则下载能下多少下多少

15.cert

  针对https,证书文件

16.session

  requests.session     可以免去写cookies

2.beautifulsoup

1.markup

  将一个字符串或者文件序列化(url,文件路径等)

2.features

  解析器类型

  

基本应用

.tag

1)name

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag = soup.b
print(tag.name)

通过可以通过该属性来修改标签,如果改变了tag的name,那将影响所有通过当前Beautiful Soup对象生成的HTML文档。

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
tag.name = 'a'
print(tag)

2)Attributes

一个tag可能有很多个属性. tag <b class="boldest"> 有一个 “class” 的属性,值为 “boldest” . tag的属性的操作方法与字典相同

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
print(tag['class'])

也可以使用attrs可以以字典形式返回标签的所有属性

from bs4 import BeautifulSoup
soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
print(tag.attrs)

tag的属性可以被添加,删除或修改.

tag['class'] = 'verybold'
tag['id'] = 1 del tag['class']
del tag['id'] tag['class']
print(tag.get('class'))

3)children

所有子标签

4)clear

将标签的所有子标签全部清空(保留标签名)

tag = soup.find('body')
tag.clear()
print(soup)

5)decompose

递归的删除所有的标签

body = soup.find('body')
body.decompose()
print(soup)

6)extract

递归的删除所有的标签,并获取删除的标签

body = soup.find('body')
v = body.extract()
print(soup)

7)decode 和 encode

decode转换为字符串(含当前标签);decode_contents(不含当前标签)

encode转换为字节(含当前标签);encode_contents(不含当前标签)

body = soup.find('body')
v = body.decode()
v = body.decode_contents()
print(v)

decode

body = soup.find('body')
v = body.encode()
v = body.encode_contents()
print(v)

encode

8)find 和 find_all

查找第一个和查找所有,源码中find的实现基于find_all,取[0]

tag = soup.find('a')
print(tag)
tag = soup.find(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie')
tag = soup.find(name='a', class_='sister', recursive=True, text='Lacie')
print(tag)

find

tags = soup.find_all('a')
print(tags) tags = soup.find_all('a',limit=1)
print(tags) tags = soup.find_all(name='a', attrs={'class': 'sister'}, recursive=True, text='Lacie')
tags = soup.find(name='a', class_='sister', recursive=True, text='Lacie')
print(tags) ####### 列表 #######
v = soup.find_all(name=['a','div'])
print(v) v = soup.find_all(class_=['sister0', 'sister'])
print(v) v = soup.find_all(text=['Tillie'])
print(v, type(v[0])) v = soup.find_all(id=['link1','link2'])
print(v) v = soup.find_all(href=['link1','link2'])
print(v) ####### 正则 #######
import re
rep = re.compile('p')
rep = re.compile('^p')
v = soup.find_all(name=rep)
print(v) rep = re.compile('sister.*')
v = soup.find_all(class_=rep)
print(v) rep = re.compile('http://www.oldboy.com/static/.*')
v = soup.find_all(href=rep)
print(v) ####### 方法筛选 #######
def func(tag):
return tag.has_attr('class') and tag.has_attr('id')
v = soup.find_all(name=func)
print(v) ## get,获取标签属性
tag = soup.find('a')
v = tag.get('id')
print(v)

find_all

9)has_attr

检查标签是否具有该属性

10)get_text

获取标签内部文本内容

11)index

检查标签在某标签中的索引位置

爬虫 1 requests 、beautifulsoup的更多相关文章

  1. 【Python】在Pycharm中安装爬虫库requests , BeautifulSoup , lxml 的解决方法

    BeautifulSoup在学习Python过程中可能需要用到一些爬虫库 例如:requests BeautifulSoup和lxml库 前面的两个库,用Pychram都可以通过 File--> ...

  2. 爬虫之Requests&beautifulsoup

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

  3. python 爬虫(一) requests+BeautifulSoup 爬取简单网页代码示例

    以前搞偷偷摸摸的事,不对,是搞爬虫都是用urllib,不过真的是很麻烦,下面就使用requests + BeautifulSoup 爬爬简单的网页. 详细介绍都在代码中注释了,大家可以参阅. # -* ...

  4. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  5. 利用requests, beautifulsoup包爬取股票信息网站

    这是第一次用requests, beautifulsoup实现爬虫,此次爬取的是一个股票信息网站:http://www.gupiaozhishi.net.cn. 实现非常简单,只是为了demo使用的数 ...

  6. Python爬虫练习(requests模块)

    Python爬虫练习(requests模块) 关注公众号"轻松学编程"了解更多. 一.使用正则表达式解析页面和提取数据 1.爬取动态数据(js格式) 爬取http://fund.e ...

  7. 爬虫入门二 beautifulsoup

    title: 爬虫入门二 beautifulsoup date: 2020-03-12 14:43:00 categories: python tags: crawler 使用beautifulsou ...

  8. 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用

    [爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...

  9. 【网络爬虫入门03】爬虫解析利器beautifulSoup模块的基本应用

    [网络爬虫入门03]爬虫解析利器beautifulSoup模块的基本应用   1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.B ...

  10. Python爬虫之requests

    爬虫之requests 库的基本用法 基本请求: requests库提供了http所有的基本请求方式.例如 r = requests.post("http://httpbin.org/pos ...

随机推荐

  1. UVa839

    这个引用好精髓. #include <iostream> #include <cstring> #include <string> #include <map ...

  2. Codeforces1076F. Summer Practice Report(贪心+动态规划)

    题目链接:传送门 题目: F. Summer Practice Report time limit per test seconds memory limit per test megabytes i ...

  3. vue中使用axios给生产环境和开发环境配置不同的baseUrl

    第一步:设置不同的接口地址 找到文件:/config/dev.env.js 代码修改为: var merge = require('webpack-merge') var prodEnv = requ ...

  4. go-json处理的问题

    1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...

  5. Python2和Python3关于reload()用法的区别

    Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) >>> from imp >>> imp. ...

  6. Nginx自动安装脚本

    添加一个install_nginx.sh脚本 版本一:(以下脚本为在线自动化安装) #!/bin/bash mkdir /soft cd /soft wget -c http://nginx.org/ ...

  7. Linux双线双网卡双IP双网关设置方法

    机房上架了一台测试机,系统是Ubuntu 9.04 X64的系统,母机IBM X336机器.用户需求是双线,故采用一个网卡配置电信地址,另一个网卡配置联通地址,安装好系统后配置好IP发现联通地址和电信 ...

  8. css 易错点总结

    心得:思路清晰,细致,耐心. 慢慢来,先规划,再动手,先整体后局部,规划好整个页面先. 命名合理,且小心重复 防止标签嵌套错误,以及忘记闭合 行高要在字体后面,如下: 正确:font:400 15px ...

  9. ubuntu拒绝root用户ssh远程登录解决办法

    ubuntu拒绝root ssh远程登录通常情况是ssh设置了禁止root远程登录,解决办法就是:修改ssh配置,然后重启ssh服务即可. vi /etc/ssh/sshd_config 找到并用#注 ...

  10. 1.2.4 Excel快速建立n个文件夹

    1.准备员工信息表,选中名字 2.[设置单元格格式]>[数字]>[自定义]>右侧的[类型]>输入”md ”@>单击[确定] 3.确定后在姓名前会出现md,新建文本文档,将 ...