21天打造分布式爬虫-requests库(二)
2023-09-11 14:19:33 时间
2.1.get请求
简单使用
import requests response = requests.get("https://www.baidu.com/") #text返回的是unicode的字符串,可能会出现乱码情况 # print(response.text) #content返回的是字节,需要解码 print(response.content.decode('utf-8')) # print(response.url) #https://www.baidu.com/ # print(response.status_code) #200 # print(response.encoding) #ISO-8859-1
添加headers和params
import requests params = { 'wd':'python' } headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36' } response = requests.get("https://www.baidu.com/s",params=params,headers=headers) #content返回的是字节,需要解码 with open('baidu.html','w',encoding='utf-8') as f: f.write(response.content.decode('utf-8'))
2.2.POST请求
爬去拉钩网职位信息
import requests url = "https://www.lagou.com/jobs/positionAjax.json?city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false" data = { 'first':'true', 'pn':1, 'kd':'python' } headers = { "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36", "Referer":"https://www.lagou.com/jobs/list_python?city=%E5%8C%97%E4%BA%AC&cl=false&fromSearch=true&labelWords=&suginput=" } response = requests.post(url,data=data,headers=headers) # print(response.text) print(type(response.text)) #<class 'str'> print(type(response.json())) #<class 'dict'> print(response.json()) #获取为字典的形式
2.3.使用代理
import requests proxy = {'http':'115.210.31.236.55:9000'} response = requests.get("https://www.baidu.com/",proxies=proxy) print(response.content.decode('utf-8'))
2.4.session登录
# _*_ coding:utf-8 _*_ import requests # 1. 创建session对象,可以保存Cookie值 ssion = requests.session() # 2. 处理 headers headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'} # 3. 需要登录的用户名和密码 data = {"email":"158xxxxxxxx", "password":"pythonxxxxxxx"} # 4. 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里 ssion.post("http://www.renren.com/PLogin.do", data = data) # 5. ssion包含用户登录后的Cookie值,可以直接访问那些登录后才可以访问的页面 response = ssion.get("http://zhibo.renren.com/news/108") # 6. 打印响应内容 print(response.text)
相关文章
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
- 第三百五十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)介绍以及安装
- 第三百五十七节,Python分布式爬虫打造搜索引擎Scrapy精讲—利用开源的scrapy-redis编写分布式爬虫代码
- 第三百五十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy分布式爬虫要点
- 第三百五十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解
- 第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中
- 第三百四十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—Requests请求和Response响应介绍
- 第三百四十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫数据保存
- 第三百四十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—编写spiders爬虫文件循环抓取内容—meta属性返回指定值给回调函数—Scrapy内置图片下载器
- scrapy框架使用-scrapy-redis的使用,通过requests去重实现增量式爬虫,使用redisspider实现分布式爬虫
- 4-爬虫框架-分布式异步爬虫
- 小白学 Python 爬虫(13):urllib 基础使用(三)
- Python 爬虫入门实战
- 分布式爬虫部署,爬虫需要什么样的服务器配置,现在爬虫岗位都要会这个
- 4-爬虫框架-分布式异步爬虫
- Python爬虫开发:requests库的使用--session的使用
- 【收藏】八爪鱼爬虫软件教程(7):新手入门-自定义模式
- 13天搞定python分布式爬虫(视频+项目双管齐下)
- 爬虫数据清洗
- 使用 CasperJS 构建 Web 爬虫
- Python爬虫:利用aiowebsocket库抓取WebSocket数据
- scrapy-redis实现爬虫分布式爬取分析与实现
- 爬虫日记(64):Scrapy的设置参数-统计分析
- Crawler:反爬虫之基于urllib库+伪装浏览器方式实现从各种网站上(以百度贴吧为例)获得你喜欢的照片下载到本地电脑上