爬虫笔记01--curl,wget,urllib,requests简单使用方法
2020-01-12 22:49
176 查看
苦于知识学了又忘,忘了有学,好记性不如烂笔头吧。记录一下。
curl指令和wget命名没有安装centos版本可以用下面的命令。
yum install curl yum install wget
注意:安装的时候可能会遇到报错,有可能是openssl没装,
yum install openssl yum install openssl-dev
如果是ubuntu系统把yum改成apt即可。
curl
一些常用参数的用法
参数 | 说明 | 示例 |
---|---|---|
-A | 设置user-agent | curl -A “Chrome” http://www.baidu.com |
-X | 用指定方法请求 | curl -X POST http://httpbin.org/post |
-I | 只返回请求的头信息 | |
-d | 以POST方法请求url,并发送相应的参数 | -d a=1 -d b=2 -d c=3 -d “a=1&b=2&c=3” -d @filename |
-O | 下载文件并以远程的文件名保存 | |
-o | 下载文件并以指定的文件名保存 | curl -o fox.jpeg http://httpbin.org/image/jpeg |
-L | 跟随重定向请求 | curl -IL https://baidu.com |
-H | 设置头信息 | curl -o image.webp -H “accept:image/webp” http://httpbin.org/image |
-k | 允许发起不安全的SSL请求 | |
-b | 设置cookies | curl -b a=test http://httpbin.org/cookies |
-s | 不显示其他无关信息 | |
-v | 显示连接过程中的所有信息 |
wget
参数 | 说明 | 实例 |
---|---|---|
-O | 以指定文件名保存下载的文件 | wget -O test.png http://httpbin.org/image/png |
–limit-rate | 以指定的速度下载目标文件 | –limit-rate=200k |
-c | 断点续传 | |
-b | 后台下载 | |
-U | 设置User-Agent | |
–mirror | 镜像某个目标网站 | |
-p | 下载页面中的所有相关资源 | |
-r | 递归下载所有网页中所有的链接 |
镜像下载整个网站并保存到本地
wget -c --mirror -U "Mozilla" -p --convert-links http://docs.python-requests.org
urllib学习:
# 接收一个字符串作为参数 import urllib.request r = urllib.request.urlopen("http://httpbin.org/get") text = r.read() #打印read的内容 print(text) # http返回状态码和msg print(r.status, r.reason)
# 返回的内容是json格式,直接用Load函数加载 obj = json.loads(text) print(obj)
# r.headers是一个HTTPMessage对象 print(r.headers) print("="*30) for k, v in r.headers._headers: print("%s , %s" % (k, v))
print("="*30) # 添加自定义的头信息 ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) ' \ 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77' \ ' Safari/537.36' # 接收一个urllib.request.Request对象作为参数 req = urllib.request.Request("http://httpbin.org/user-agent") # 使用GET参数 req.add_header("User-Agent", ua) r = urllib.request.urlopen(req).read().decode("utf-8") obj = json.loads(r) # print(obj) print("User-Agent:", obj['user-agent'])
说一下json模块下的load和load的小区别:load使用时读取是标准json文件,loads可以读取任意类json字符串格式文件。再进行数据获取操作。
# 使用POST方法传递参数 data=urllib.parse.urlencode({"name": "wjl", "age": "25"}) data=data.encode() with urllib.request.urlopen("http://httpbin.org/post", data) as f: print(json.load(f))
# 使用代理IP请求远程url proxy_handler = urllib.request.ProxyHandler({ 'http': 'http://wjl.com.cn:670'#自己的代理ip端口 }) # proxy_auth_handler = urllib.request.ProxyBasicAuthHandler() opener = urllib.request.build_opener(proxy_handler) r = opener.open('http://httpbin.org/ip') print(r.read())
# urlparse模块 params = urllib.parse.urlparse('http://httpbin.org/get') print(params)
Request:
import requests # GET请求 r = requests.get('http://httpbin.org/get') print(r.status_code, r.reason) print('GET请求', r.text) # 带参数的GET请求 r = requests.get('http://httpbin.org/get', params={'a': '1', 'b': '2'}) print('带参数的GET请求', r.json()) #POST请求 r = requests.post('http://httpbin.org/post', data={'a': '1'}) print('POST请求', r.json()) # 自定义headers请求 ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) ' \ 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77' \ ' Safari/537.36' headers = {'User-Agent': ua} r = requests.get('http://httpbin.org/headers', headers=headers) print('自定义headers请求', r.json()) # 带cookies的请求 cookies = dict(userid='123456', token='xxxxxxxxxxxxxxxxxxxx') r = requests.get('http://httpbin.org/cookies', cookies=cookies) print('带cookies的请求', r.json()) # Basic-auth认证请求 r = requests.get('http://httpbin.org/basic-auth/guye/123456', auth=('guye', '123456')) print('Basic-auth认证请求', r.json()) # 主动抛出状态码异常 bad_r = requests.get('http://httpbin.org/status/404') print(bad_r.status_code) bad_r.raise_for_status() #使用requests.Session对象请求 # 创建一个Session对象 s = requests.Session() # session对象会保存服务器返回的set-cookies头信息里面的内容 s.get('http://httpbin.org/cookies/set/userid/123456789') s.get('http://httpbin.org/cookies/set/token/xxxxxxxxxxxxxxxxxx') # 下一次请求会将本地所有的cookies信息自动添加到请求头信息里面 r = s.get('http://httpbin.org/cookies') print('检查session中的cookies', r.json()) # 在requests中使用代理 print('不使用代理:', requests.get('http://httpbin.org/ip').json()) print('使用代理:', requests.get( 'http://httpbin.org/ip', proxies={'http': 'http://iguye.com:41801'} ).json()) r = requests.get('http://httpbin.org/delay/4', timeout=5) print(r.text)
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- ubuntu笔记之wget使用方法
- 在python3中使用urllib.request编写简单的网络爬虫
- APIDemo学习笔记——Android上几种简单的Animation使用方法(一)
- 爬虫requests对post的使用方法
- python2.7 httplib, urllib, urllib2, requests 库的简单使用
- Python爬虫之urllib简单使用
- JAVA基础学习笔记 day003_01 Scanner语法使用方法
- 【用Python写爬虫】获取html的方法【一】:使用urllib
- 学习笔记(01):Python可以这样学(第一季:Python内功修炼)-版本介绍、安装与简单使用...
- 在python3中使用urllib.request编写简单的网络爬虫
- Python中使用urllib2模块编写爬虫的简单上手示例
- 阿里云开发笔记01——CuteFTP使用方法
- Mars Android视频学习笔记——01_16_SQLite使用方法
- python2.7 httplib, urllib, urllib2, requests 库的简单使用
- Python爬虫(入门+进阶)学习笔记 1-3 使用Requests爬取豆瓣短评
- 爬虫笔记——东方财富科创板数据爬取(requests方法)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- PHP中使用curl伪造IP的简单方法
- shell: wget、curl(libcurl)使用方法及其区别