1.京东商品页面爬取

打开某一个京东页面

https://item.jd.com/69336974189.html

代码:

import requests
url="https://item.jd.com/69336974189.html"
try:r=requests.get(url)r.raise_for_status()r.encoding=r.apparent_encodingprint(r.text[:1000])
except:print("爬取失败!")

2.亚马逊商品页面的爬取

打开一个亚马逊链接

>>> import requests
>>> r = requests.get("https://www.amazon.cn/gp/product/B01M8L5Z3Y")
>>> r.status_code
200
>>> r.encoding
'UTF-8'
>>> r.encoding = r.apparent_encoding
>>> r.text>>> r.request.headers
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> kv ={'user-agent':'Mozilla/5.0'}
>>> url = "https://www.amazon.cn/gp/product/B01M8L5Z3Y"
>>> r = requests.get(url,headers = {'user-agent':'Mozilla/5.0'}) #此处将浏览器端口改为'user-agent':'Mozilla/5.0'
>>> r,status_code
Traceback (most recent call last):File "<pyshell#22>", line 1, in <module>r,status_code
NameError: name 'status_code' is not defined #此处代码报错,一定要注意代码的规范书写,快找找错误在哪里
>>> r.status_code
200
>>> r.request.headers
{'user-agent': 'Mozilla/5.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> r.text[:1000]
'\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n\n\n    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    <!doctype html><html class="a-no-js" data-19ax5a9jf="dingo">\n    <head>\n<script type="text/javascript">var ue_t0=ue_t0||+new Date();</script>\n<script type="text/javascript">\nwindow.ue_ihb = (window.ue_ihb || window.ueinit || 0) + 1;\nif (window.ue_ihb === 1) {\nvar ue_hob=+new Date();\nvar ue_id=\'E06HEJQW1W99HDZ0Z5H0\',\nue_csm = window,\nue_err_chan = \'jserr-rw\',\nue = {};\n(function(d){var e=d.ue=d.ue||{},f=Date.now||function(){return+new Date};e.d=function(b){return f()-(b?0:d.ue_t0)};e.stub=function(b,a){if(!b[a]){var c=[];b[a]=function(){c.push([c.slice.call(arguments),e.d(),d.ue_id])};b[a].replay=function(b){for(var a;a=c.shift();)b(a[0],a[1],a[2])};b[a].isStub=1}};e.exec=function(b,a){return function(){try{return b.apply(this,arguments)}catch(c){ueLogError(c,{attribution:a||"undefined",logLevel:"WARN"})}}}})(ue_csm);\n\nue.stub(ue,"log");ue.stub(ue,"onunload");ue.stu'
>>> 

全部代码:

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_encodingprint(r.text[1000:2000])
except:print("爬取失败")

3,百度/360搜索关键词提交

搜索引擎关键词提交接口
百度的关键词接口:
http://www.baidu.com/s?wd=keyword

>>> import requests
>>> kv = {'wd':'Python'}
>>> r = requests.get("http://www.baidu.com/s",params=kv)
>>> r.status_code
200
>>> r.request.url
'http://www.baidu.com/s?wd=Python'
>>> len(r.text)
524221

代码:

import requests
keyword = "Python"
try:kv = {'wd':keyword}r = requests.get("http://www.baidu.com/s",params=kv)print(r.request.url)r.raise_for_status()print(len(r.text))
except:print("爬取失败")

360的关键词接口:
http://www.so.com/s?q=keyword

代码:

import requests
keyword = "Python"
try:kv ={'q':keyword}r = requests.get("http://www.so.com/s",params=kv)print(r.request.url)r.raise_for_status()print(r.text)
except:print("爬取失败")

4.网络图片的爬取和存储

网络图片链接的格式:
http://www.example.com/picture.jpg

比如:

http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg

import requests
import os
url = "http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"
root = "D://MyProject//Python学习//爬虫学习//"
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("爬取失败")

5.IP地址归属地的自动查询

import requests
url = "http://ip.ws.126.net/ipquery?ip="
try:r = requests.get(url+'202.204.80.112')r.raise_for_status()r.encoding = r.apparent_encodingprint(r.text[-500:])
except:print("爬取失败 ")

【Python爬虫】Requests库网络爬虫实战相关推荐

  1. Python网络爬虫与信息提取 第1周网络爬虫之规则 单元3:Requests库网络爬虫实战

    京东商品页面的爬取 >>> import requests >>> r = requests.get("https://item.jd.com/29679 ...

  2. Requests库网络爬虫实战

    实例一:页面的爬取 >>> import requests >>> r= requests.get("https://item.jd.com/100003 ...

  3. python爬虫requests库_Python爬虫(三)Requests库

    什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库.与urllib相比,Requests更加方便,可以节约 ...

  4. python爬虫requests库_python爬虫基础教程:requests库(二)代码实例

    get请求 简单使用 import requests ''' 想要学习Python?Python学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载! ''' respons ...

  5. python爬虫requests库_python爬虫使用Requests库 - pytorch中文网

    在入门教程中我们介绍了urllib库和urllib2的用法,同时我们了解一些爬虫的基础以及对爬虫有了基本的了解.其实在我们生产环境中,使用Request库更加方便与实用,同时我们这需要短短的几行代码就 ...

  6. Python 的 requests 库的用法

    Python爬虫利器一之Requests库的用法:http://cuiqingcai.com/2556.html Python利用Requests库写爬虫(一):http://www.jianshu. ...

  7. python爬虫requests实战_Python爬虫之requests库网络爬取简单实战

    实例1:直接爬取网页 实例2 : 构造headers,突破访问限制,模拟浏览器爬取网页 实例3 : 分析请求参数,构造请求参数爬取所需网页 实例4: 爬取图片 实例5: 分析请求参数,构造请求参数爬取 ...

  8. python网络爬虫系列教程——python中requests库应用全解

    全栈工程师开发手册 (作者:栾鹏) python教程全解 python中requests库的基础应用,网页数据挖掘的常用库之一.也就是说最主要的功能是从网页抓取数据. 使用前需要先联网安装reques ...

  9. Python网络爬虫之requests库Scrapy爬虫比较

    requests库Scrapy爬虫比较 相同点: 都可以进行页面请求和爬取,Python爬虫的两个重要技术路线 两者可用性都好,文档丰富,入门简单. 两者都没有处理JS,提交表单,应对验证码等功能(可 ...

最新文章

  1. 模拟请求分页管理中地址转换和缺页中断处理_Linux内存管理:缺页异常(一)
  2. Java Web Application 自架构 四 Log4j2日志管理
  3. 少量数据训练语音识别的思路
  4. WinCE开发初探——开发环境
  5. 业内首款云原生技术中台产品云原生 Stack 来了!
  6. websettings 哪里设置_云浮超级电容用石墨哪里买,可膨胀石墨_青岛天源达
  7. Python中is与==的使用区别详解
  8. linux CentOS7最小化安装环境静默安装Oracle11GR2数据库(配置数据库监听_09)
  9. 09-解决服务器被黑上不了网的问题
  10. (转)2017中国互联网证券年度报告
  11. 容器技术Docker K8s 30 容器服务ACK基础与进阶-弹性伸缩
  12. 《编程之美》---求二叉树中节点的最大距离
  13. 视频md5修改器苹果手机
  14. java db4o,DB4O--java对象数据库
  15. Qt中清空layout中所有控件
  16. 图像的采样、频谱和分辨率
  17. python猴子分桃_Python 五猴分桃.py问题解答代码
  18. 什么是责任心?如何提高责任心?HR人才测评
  19. android vold磁盘管理
  20. 二叉排序树和平衡二叉树

热门文章

  1. MySQL性能优化(七)-- 慢查询
  2. 阿里云 Centos 7 PHP7环境配置 LNMP
  3. 谷歌浏览器的驱动下载安装与配置-0223
  4. python-描述符的操作
  5. bootstrap-表单
  6. linux-远程管理-xshell
  7. Tidb慢日志显示不全
  8. 超简单利用xposed框架破解钉钉打卡
  9. OpenCV 入门级一
  10. WPF依赖属性(续)(1)