python爬虫入门--Requests库介绍及实例
2017-03-18 21:06
901 查看
1、Requests库基础知识
Requests库的get()方法
Requests库的Response对象:
Response对象包含服务器返回的所有信息,也包含请求的Request信息。
运行截图如下所示:
2、Requests库实例
(1)京东商品的爬取--普通爬取框架
(2)亚马逊商品的爬取--通过修改headers字段,模拟浏览器向网站发起请求
(3)百度/360搜索关键词提交--修改params参数提交关键词
百度的关键词接口:http://www.baidu.com/s?wd=keyword
360的关键词接口:http://www.so.com/s?q=keyword
(4)网络图片的爬取和存储--结合os库和文件操作的使用
Requests库的get()方法
Requests库的Response对象:
Response对象包含服务器返回的所有信息,也包含请求的Request信息。
运行截图如下所示:
2、Requests库实例
(1)京东商品的爬取--普通爬取框架
import requests url = "https://item.jd.com/2967929.html" try: r = requests.get(url) r.raise_for_status() r.encoding = r.apparent_encoding print(r.text[:1000]) except: print("爬取失败!")
(2)亚马逊商品的爬取--通过修改headers字段,模拟浏览器向网站发起请求
import requests url="https://www.amazon.cn/gp/product/B01M8L5Z3Y" try: kv = {'user-agent':'Mozilla/5.0'} r=requests.get(url,headers=kv) r.raise_for_status() r.encoding=r.apparent_encoding print(r.status_code) print(r.text[:1000]) except: print("爬取失败")
(3)百度/360搜索关键词提交--修改params参数提交关键词
百度的关键词接口:http://www.baidu.com/s?wd=keyword
360的关键词接口:http://www.so.com/s?q=keyword
import requests url="http://www.baidu.com/s" try: kv={'wd':'Python'} r=requests.get(url,params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) print(r.text[500:5000]) except: print("爬取失败")
(4)网络图片的爬取和存储--结合os库和文件操作的使用
import requests import os url="http://tc.sinaimg.cn/maxwidth.800/tc.service.weibo.com/p3_pstatp_com/6da229b421faf86ca9ba406190b6f06e.jpg" root="D://pics//" path=root + url.split('/')[-1] try: if not os.path.exists(root): os.mkdir(root) if not os.path.exists(path): r = requests.get(url) with open(path, 'wb') as f: f.write(r.content) f.close() print("文件保存成功") else: print("文件已存在") except: print("爬取失败")
相关文章推荐
- python爬虫入门--Beautiful Soup库介绍及实例
- [Python爬虫] scrapy爬虫系列 <一>.安装及入门介绍
- Python爬虫爬取深大信息工程学院老师个人介绍网址小实例
- python爬虫从入门到放弃(四)之 Requests库的基本使用
- Python爬虫天气预报实例详解(小白入门)
- [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
- Python爬虫入门 第一章 Requests库入门
- python爬虫从入门到放弃(九)之 实例爬取上海高级人民法院网开庭公告数据
- Python入门(一):爬虫基本结构&简单实例
- 学习Python爬虫(三):Requests库入门级使用
- python爬虫入门笔记:Requests库
- python 网络爬虫入门(一)———第一个python爬虫实例
- python 爬虫系列(1) --- requests库入门
- [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
- 【Python数据挖掘课程】一.安装Python及爬虫入门介绍
- Python爬虫小白入门(二)requests库
- Python爬虫入门(二)requests库
- python爬虫入门(六) Scrapy框架之原理介绍
- python3 爬虫入门(二)requests库基本使用
- python爬虫入门笔记:Requests库