爬虫-Requests 使用入门
requests 的底层实现其实就是 urllib
json在线解析工具
----------------------------------------------
Linux alias命令用于设置指令的别名。
home目录中~/.bashrc 这个文件主要保存个人的一些个性化设置,如命令别名、路径等。
注意:1,写绝对路径
2,有空格

改好后使用source ~/.bashrc 使用文件生效
------------------------------------------------
assert response.status_code==200
assert response.status_code==200
raise异常
raise 引发一个异常
例子:如果输入的数据不是整数,则引发一个ValueError
inputValue=input("please input a int data :")
if type(inputValue)!=type(1):
raise ValueError
else:
print inputValue
假设输入1.2,运行结果为:
please input a int data :1.2
Traceback (most recent call last):
File "C:/Users/lirong/PycharmProjects/untitled/openfile.py", line 3, in <module>
raise ValueError
ValueError
如果输入1,运行结果为:
please input a int data :1
url编码
https://www.baidu.com/s?wd=%E4%BC%A0%E6%99%BA%E6%92%AD%E5%AE%A2
字符串格式化的另一种方式
"传{}智播客".format(1)
使用代理ip
代理IP百度有很多,推荐使用高匿IP
用法:requests.get("http://www.baidu.com", proxies = proxies)
proxies的形式:字典
proxies = {
"http": "http://12.34.56.79:9527",
"https": "https://12.34.56.79:9527",
}
问题:为什么爬虫需要使用代理? 让服务器以为不是同一个客户端在请求 防止我们的真实地址被泄露,防止被追究
准备一堆的ip地址,组成ip池,随机选择一个ip来时用
如何随机选择代理ip,让使用次数较少的ip地址有更大的可能性被用到
{"ip":ip,"times":0}
[{},{},{},{},{}],对这个ip的列表进行排序,按照使用次数进行排序
选择使用次数较少的10个ip,从中随机选择一个
检查ip的可用性
可以使用requests添加超时参数,判断ip地址的质量
在线代理ip质量检测的网站
携带cookie请求
携带一堆cookie进行请求,把cookie组成cookie池
使用requests提供的session类来请求登陆之后的网站的思路
requests 提供了一个叫做session类,来实现客户端和服务端的会话保持
使用方法:
1 实例化一个session对象
2 让session发送get或者post请求
session = requests.session()
response = session.get(url,headers)
实例化session
先使用session发送请求,登录对网站,把cookie保存在session中
再使用session请求登陆之后才能访问的网站,session能够自动的携带登录成功时保存在其中的cookie,进行请求
不发送post请求,使用cookie获取登录后的页面
cookie过期时间很长的网站
在cookie过期之前能够拿到所有的数据,比较麻烦
配合其他程序一起使用,其他程序专门获取cookie,当前程序专门请求页面
字典推导式,列表推导式
cookies="anonymid=j3jxk555-nrn0wh; _r01_=1; _ga=GA1.2.1274811859.1497951251;
_de=BF09EE3A28DED52E6B65F6A4705D973F1383380866D39FF5; ln_uact=mr_mao_hacker@163.com; depovince=BJ;
jebecookies=54f5d0fd-9299-4bb4-801c-eefa4fd3012b|||||; JSESSIONID=abcI6TfWH4N4t_aWJnvdw;
ick_login=4be198ce-1f9c-4eab-971d-48abfda70a50; p=0cbee3304bce1ede82a56e901916d0949; first_login_flag=1;
ln_hurl=http://hdn.xnimg.cn/photos/hdn421/20171230/1635/main_JQzq_ae7b0000a8791986.jpg;
t=79bdd322e760beae79c0b511b8c92a6b9; societyguester=79bdd322e760beae79c0b511b8c92a6b9;
id=327550029; xnsid=2ac9a5d8; loginfrom=syshome; ch_id=10016; wp_fold=0"
cookies = {i.split("=")[0]:i.split("=")[1] for i in cookies.split("; ")}
[self.url_temp.format(i * 50) for i in range(1000)]
获取登录后的页面的三种方式
实例化session,使用session发送post请求,在使用他获取登陆后的页面
import requests session = request.session()
post_url = ""
post_data = {"email":"xx@163.com","password":"xxx"}
headers = {
...}
# 使用session发送post请求,cookie保存在其中
session.post(post_url,data=post_data,headers=headers)
#在使用session进行请求登陆之后才能访问的地址
r = session.get("http://www.renren.com/327550029/profile",headers=headers) #保存页面
with open("renren1.html","w",encoding="utf-8") as f:
f.write(r.content.decode())
headers中添加cookie键,值为cookie字符串
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36",
"Cookie":...
}
r = requests.get("http://www.renren.com/327550029/profile",headers=headers)
#保存页面
with open("renren2.html","w",encoding="utf-8") as f:
f.write(r.content.decode())
在请求方法中添加cookies参数,接收字典形式的cookie。字典形式的cookie中的键是cookie的name对应的值,值是cookie的value对应的值
# coding=utf-
import requests headers = {...,} cookies="anonymid=j3jxk555-nrn0wh; ..."
cookies = {i.split("=")[]:i.split("=")[] for i in cookies.split("; ")}
print(cookies) r=requests.get("http://...",headers=headers,cookies=cookies) #保存页面
with open("renren3.html","w",encoding="utf-8") as f:
f.write(r.content.decode())
-------------------------------------------
response = requests.url(...)
response.encoding # 查看编码
response.encoding = 'utf-8' # 指定编码
response.content.decode() # 将二进制的获取网页数据返回到本地进行解码 另两种方法 response.content.decode('gbk') response.text
爬虫-Requests 使用入门的更多相关文章
- Python 爬虫-Requests库入门
2017-07-25 10:38:30 response = requests.get(url, params=None, **kwargs) url : 拟获取页面的url链接∙ params : ...
- 初学Python之爬虫的简单入门
初学Python之爬虫的简单入门 一.什么是爬虫? 1.简单介绍爬虫 爬虫的全称为网络爬虫,简称爬虫,别名有网络机器人,网络蜘蛛等等. 网络爬虫是一种自动获取网页内容的程序,为搜索引擎提供了重要的 ...
- 小白学 Python 爬虫(34):爬虫框架 Scrapy 入门基础(二)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(35):爬虫框架 Scrapy 入门基础(三) Selector 选择器
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(36):爬虫框架 Scrapy 入门基础(四) Downloader Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(37):爬虫框架 Scrapy 入门基础(五) Spider Middleware
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(38):爬虫框架 Scrapy 入门基础(六) Item Pipeline
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(40):爬虫框架 Scrapy 入门基础(七)对接 Selenium 实战
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- 小白学 Python 爬虫(41):爬虫框架 Scrapy 入门基础(八)对接 Splash 实战
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
随机推荐
- ArcGis相接面补节点c#
相接(Touch)面执行切割后 新面与原相接面会缺少公共节点. private void AddPointToTouchesPolygon(IFeatureCursor newFeatureCurso ...
- C语言指针和字符串
#include <stdio.h> int main() { /********************************************* * 内存: * 1.常量区 * ...
- linux 下无法输入# 显示为£
在键盘布局里面,(Keyboard Layout)设置为中国,汉语.解决问题
- 逻辑右移函数 srl()与算术右移函数 sra() (转)
比如一个有符号位的8位二进制数11001101,逻辑右移就不管符号位,如果移一位就变成01100110.算术右移要管符号位,右移一位变成10100110. 逻辑左移=算数左移,右边统一添0 逻辑右移, ...
- JS window对象取消计时器clearInterval() clearInterval() 方法可取消由 setInterval() 设置的交互时间。
取消计时器clearInterval() clearInterval() 方法可取消由 setInterval() 设置的交互时间. 语法: clearInterval(id_of_setInterv ...
- TFS发布的时候出现 ENOENT: no such file or directory, stat 'E:\vsts-agent\_work\r57\a\KingEagle-Mysql-Dev\drop\12917.zip' 解决方案
出现 ENOENT: no such file or directory, stat 'E:\vsts-agent\_work\r57\a\KingEagle-Mysql-Dev\drop\12917 ...
- 日文NLP分词系统
mecab(http://mecab.sourceforge.net/)是奈良先端科技大学开发的日文分词系统,基于CRF的分词原理,有c++实现,提供python.perl.ruby等接口 日文NLP ...
- 36. 解决线程问题方式一(同步代码块synchronized)
解决线程问题: 方式一:同步代码块(synchronized) 语法: synchronized ("锁对象") { //需要锁定的代码 } ...
- CF838C(博弈+FWT子集卷积+多项式ln、exp)
传送门: http://codeforces.com/problemset/problem/838/C 题解: 如果一个字符串的排列数是偶数,则先手必胜,因为如果下一层有后手必赢态,直接转移过去,不然 ...
- lsof 详解
lsof常用参数 lsof 如果不加任何参数,就会打开所有被打开的文件,建议加上一下参数来具体定位lsof -i 列出所有网络连接lsof -i tcp 列出所有tcp连接信息lsof -i udp ...