爬虫中什么是requests
print(response.text) #响应的信息
print(response.headers) #获取响应头
print(response.status_code) #响应状态码
print(response.encoding) #响应的编码
print(response.cookies) #获取cookies信息
带参数GET请求
data = {
'name':'abc',
''''''
}
response = requests.get(url='http://www.baidu.com',params=data)
解析json
import requests
response = requests.get(url='http://www.baidu.com')
print(response.json())
获取二进制数据
import requests
response = requests.get(url='http://www.baidu.com')
print(response.content)
高级操作
文件上传
import requests
flies = {
'flies':open('XXX','rb')
}
response = requests.post(url='http://www.baidu.com',flies=flies)
print(response.content)
会话维持 (模拟登陆)
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
{
"cookies": {
"number": "123456789"
}
}
证书验证
import requests
import urllib3
url = 'https://www.biqudu.com/43_43821/2520338.html'
urllib3.disable_warnings() #关闭证书后再把警告提示关闭
response = requests.get(url=url,verify=False)
print(response.text)
代理认证
url = 'https://www.biqudu.com/43_43821/2520338.html'
proxies = {
'http':'http://127.0.0.2',
'https':'http://user:pwd@127.0.0.2', #带密码的代理
}
response = requests.get(url=url,proxies=proxies)
print(response.text)
****
请求超时处理
import requests
from requests.exceptions import ReadTimeout #导入错误模块
url = 'https://www.taobao.com'
try:
response = requests.get(url=url,timeout=0.1) #限制请求时间
print(response.status_code)
except ReadTimeout:
print('请求超时')
认证设置
#有的网站打开的瞬间就需要密码认证
import requests
from requests.auth import HTTPBasicAuth
url = 'https://www.taobao.com'
response = requests.get(url=url,auth=('user','pwd'))
print(response.status_code)
1,笔趣阁小说(入门级爬取文本信息)
抓取笔趣阁小说:排行榜单的小说总榜
1.请求初始url,获取网页源码
2.解析网页源码,得到文本内容
3.将小说全部章节名存入txt文件中
from lxml import etree
import requests
url = 'http://www.biqiuge.com/paihangbang'
response = requests.get(url)
response.encoding = response.apparent_encoding
html = etree.HTML(response.text)
info = html.xpath("//div[@class='block bd'][1]/ul[@class='tli']/li/a")
for i in info:
title = i.xpath("./text()")[0]
urls =i.xpath("./@href")[0]
urls1 = 'http://www.biqiuge.com'+urls
with open(title+'.txt','w+',encoding='utf-8') as f:
response1 = requests.get(url=urls1)
response1.encoding = response1.apparent_encoding
html = etree.HTML(response1.text)
info = html.xpath("//div[@class='listmain']/dl/dd/a/text()")[6:]
for i in info:
f.write(i.strip()+'\n')
print(title+"------写入成功")
------------------------------------------------------
判断路径是否存在,自动创建!!!
if not os.path.exists(title):
os.mkdir(title)
path = os.path.join(title,title1)
if not os.path.exists(path):
os.mkdir(path)
with open(path+ '\\' + title2 +'.txt', 'w+', encoding='utf-8') as f:
for con in contents:
f.write(con.strip() + '\n')
print(title +'---'+ title1 +'---'+ title2 + '---写入成功')
2,崔庆才博客(伪造头信息爬取策略)
from lxml import etree
import requests
n = 0
with open('cuijincai.txt', 'w+', encoding='utf-8') as f:
for i in range(1,10):
url = 'https://cuiqingcai.com/category/technique/python/page/'+str(i)
#这里的循环,该网站是动态显示,可以在f12/network中XHR中查到该链接url。
headers = {
Referer: https://cuiqingcai.com/category/technique/python
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
#部分网站设置反爬机制,可以为请求头设置 信息
response = requests.get(url=url,headers=headers)
html = etree.HTML(response.text)
all_div = html.xpath("//article[@class='excerpt']")
for div in all_div:
title = div.xpath("./header/h2/a/text()")[0] #当前路径下的标题信息
author = div.xpath("./p[@class='auth-span']/span[@class='muted'][1]/a/text()")[0]
time = div.xpath("./p[@class='auth-span']/span[@class='muted'][2]/text()")[0]
liulanshu = div.xpath("./p[@class='auth-span']/span[@class='muted'][3]/text()")[0]
pinlun = div.xpath("./p[@class='auth-span']/span[@class='muted'][4]/a/text()")[0]
like = div.xpath("./p[@class='auth-span']/span[@class='muted'][5]/a[@id='Addlike']/span[@class='count']/text()")[0]+'喜欢'
n += 1
f.write("第{}条\t{}\t{}\t{}\t{}\t{}\t{}\n".format(n,title,author,time,liulanshu,pinlun,like))
User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、
浏览器渲染引擎、浏览器语言、浏览器插件等。
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,
服务器基此可以获得一些信息用于处理。
https://www.liaoxuefeng.com 该网站设置反爬,可以用上面设置头信息爬取
爬虫中什么是requests的更多相关文章
- 爬虫中之Requests 模块的进阶
requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...
- python爬虫学习(6) —— 神器 Requests
Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提供了你所需要的大多数 H ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍
[根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...
- 爬虫(五)requests模块2
引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/ ...
- 爬虫系列4:Requests+Xpath 爬取动态数据
爬虫系列4:Requests+Xpath 爬取动态数据 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参 ...
- Python爬虫利器一之Requests库的用法
前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...
- 网络爬虫必备知识之requests库
就库的范围,个人认为网络爬虫必备库知识包括urllib.requests.re.BeautifulSoup.concurrent.futures,接下来将结对requests库的使用方法进行总结 1. ...
- 爬虫系列(十) 用requests和xpath爬取豆瓣电影
这篇文章我们将使用 requests 和 xpath 爬取豆瓣电影 Top250,下面先贴上最终的效果图: 1.网页分析 (1)分析 URL 规律 我们首先使用 Chrome 浏览器打开 豆瓣电影 T ...
随机推荐
- 微信小程序wxss样式详解
一.wxml 界面结构wxmL比较容易理解,主要是由八大类基础组件构成: 一.视图容器(View Container): 二.基础内容(Basic Content) 组件名 说明 组件名 说明 vie ...
- 解决MySQL5.7输入show databases 不显示内容的问题
当出现输入其他命令不显示内容的时候,请检查输入语句的后面是否带上了英文输入下的分号,同时别忘了database后面还有个s. 5. 删除数据库drop database XX(数据库名);
- 1.springAOP原理分析
环境:jdk1.8 + spring boot 2.0.9.RELEASE Spring AOP的实现本质上就是代理Proxy + 一系列的拦截器 使用@Aspect,引入依赖 <depende ...
- 配置 admin 页面
创建 blog 的管理后台 首先是 blog 这个 App,其中定义了 3个 Model,分别是 Category.Post 和 Tag.先创建 admin 页面,其代码需要写到 blog/admin ...
- HashMap 怎么 hash?又如何 map?
HashMap 是 Java 中 Map 的一个实现类,它是一个双列结构(数据+链表),这样的结构使得它的查询和插入效率都很高.HashMap 允许 null 键和值,它的键唯一,元素的存储无序,并且 ...
- DELPHI ClientData使用详解
在三层结构中,TClientDataSet的地位是不可估量的,她的使用正确与否,是十分关键的,本文从以下几个方面阐述她的使用,希望对你有所帮助. 1.动态索引procedure TForm1.DBGr ...
- Java中验证编码格式的一种方法
package forlittlecatty; import java.io.File; import java.io.FileInputStream; import java.io.IOExcept ...
- v-for与v-if的优先级
原文地址 永远不要把 v-if 和 v-for 同时用在同一个元素上. 一般我们在两种常见的情况下会倾向于这样做: 为了过滤一个列表中的项目 (比如 v-for="user in users ...
- python基础知识(函数)
创建函数 def 函数名(可以选参数): 可选参数 ''' ''' 用三引号括起来的注释 说明功能和参数信息 可选参数指定函数体 执行函数程序代码 创建一个空函数 def empty(): p ...
- 使用checked和unchecked来对整数溢出进行检测和忽略
在C#中,整数int32的取值为-2147483648~2147473647(可以通过int.MaxValue和int.MinValue获得)当超出这个范围后,编译器不会进行检查,仍然会进行运算,但得 ...