爬虫实例(一)
来源:互联网 发布:淘宝热带鱼活体 编辑:程序博客网 时间:2024/06/03 16:59
1.京东商品页面的爬取
https://item.jd.com/2967929.html
代码:
import requestsurl = "https://item.jd.com/2967929.html"try: r = requests.get(url) r.raise_for_status() r.encodint = r.apparent_encoding print(r.text[:1000])except: print("爬取失败")"""结果如下:<!-- shouji --><!DOCTYPE HTML><html lang="zh-CN"><head> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>【华为荣耀8】荣耀8 4GB+64GB 全网通4G手机 魅海蓝【行情 报价 价格 评测】-京东</title> <meta name="keywords" content="HUAWEI荣耀8,华为荣耀8,华为荣耀8报价,HUAWEI荣耀8报价"/> <meta name="description" content="【华为荣耀8】京东JD.COM提供华为荣耀8正品行货,并包括HUAWEI荣耀8网购指南,以及华为荣耀8图片、荣耀8参数、荣耀8评论、荣耀8心得、荣耀8技巧等信息,网购华为荣耀8上京东,放心又轻松" /> <meta name="format-detection" content="telephone=no"> <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/2967929.html"> <meta http-equiv="mobile-agent" content="format=html5; url=//item.m.jd.com/product/2967929.html"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <link rel="canonical" href="//item.jd.com/2967929.html"/> <link rel="dns-prefetch" href="//misc.360buyimg.com"/> <link rel="dns-prefetch" href="//static.360buyimg.com"/> <link rel="dns-prefetch" href="//img10.360buyimg.com"/> <link rel="dns-pre"""
2.亚马逊商品页面的爬取
https://www.amazon.cn/gp/product/B01M8L5Z3Y
用类似的代码去爬取,如下图所示:
可以发现发生了错误,爬取失败,status_code 为503,爬取成功应该返回200。其实已经能够从服务器获得页面相关信息回来,表明爬取失败不是网络问题造成的。在网络爬虫引发的问题及限制这篇博客中,提到服务器可以通过来源审判来拒绝爬虫的访问。
我们知道 Requests 库的两个对象中,返回的 Response 对象包含了请求服务器资源的 Request 对象的所有信息。因此可以通过 Response 对象来查看我们发送请求的相关信息:
从 User-Agent 可以看出,我们发出的请求信息已经诚实地告诉服务器,这个请求来自一个 Requests 库的 python 程序。这个不在该服务器的接受范围内,因此爬取失败。
我们可以通过修改头部信息来解决这个问题:
可以看出,发送请求的头部信息已经更改,爬取成功。其中,’Mozilla/5.0’表明请求来自于某个浏览器(火狐等)。
完整代码如下:
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.encodint = r.apparent_encoding print(r.text[1000:2000])except: print("爬取失败")
3.百度/360搜索关键字提交
如何利用程序自动向搜索引擎提交关键词并获得搜索结果呢?百度和360都提供了提交关键词的接口,如下图所示,我们只需要通过改变 keyword 来构造 url,便可以提交我们想搜索的关键词并获取相关信息了。
百度搜索代码:
import requeststry: kv = {'wd':'Python'} r = requests.get("http://www.baidu.com/s", params = kv) print(r.request.url) r.raise_for_status() print(len(r.text))except: print("爬取失败")"""结果:wdir='C:/Users/ChenJX/Desktop/爬虫')http://www.baidu.com/s?wd=Python271918"""
360搜索代码:
import requeststry: kv = {'q':'Python'} r = requests.get("http://www.so.com/s", params = kv) print(r.request.url) r.raise_for_status() print(len(r.text))except: print("爬取失败")"""结果如下:https://www.so.com/s?q=Python302893"""
4.网络图片的爬取和存储
【网络图片链接的格式:http://www.example.com/picture.jpg】
用下面这个图片链接作为例子:
http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg
代码如下:
import requests, osurl="http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"root = "C:/Users/ChenJX/Desktop/爬虫"# url.split('/')[-1]表示以符号'/'分割url字符串,并返回最后一个# 分割后得到多个子字符串,返回最后一个即返回图片名称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) # 打开该文件,'wb'表以'写二进制'的模式打开 # (可以进行写操作,写的是二进制形式) with open(path, 'wb') as f: # r.content 返回的是响应内容的二进制形式 f.write(r.content) f.close() print("文件保存成功") else: print("文件已存在")except: print("爬取失败")"""结果如下:文件保存成功"""
同时可以看到该图片已经保存在相应的目录下:
5.IP地址归属地的自动查询
下图是查询 IP 地址归属地的页面,我们只需要传入 ipaddress 这个参数、构造一个 url,就可以查询归属地。
代码如下:
import requestsurl = "http://m.ip138.com/ip.asp?ip="try: r = requests.get(url + '202.204.80.112') r.raise_for_status() r.encoding = r.apparent_encoding print(r.text[-500:])except: print("爬取失败")"""结果如下:value="查询" class="form-btn" /> </form> </div> <div class="query-hd">ip138.com IP查询(搜索IP地址的地理位置)</div> <h1 class="query">您查询的IP:202.204.80.112</h1><p class="result">本站主数据:北京市海淀区 北京理工大学 教育网</p><p class="result">参考数据一:北京市 北京理工大学</p> </div> </div> <div class="footer"> <a href="http://www.miitbeian.gov.cn/" rel="nofollow" target="_blank">沪ICP备10013467号-1</a> </div> </div> <script type="text/javascript" src="/script/common.js"></script></body></html>"""
- Python爬虫实例一
- 爬虫实例(一)
- python爬虫慕课网实例(一)
- Scrapy爬虫笔记【5-实例一:爬豆瓣】
- python网络爬虫实例(一):爬取糗事百科
- Python爬虫爬取动态页面思路+实例(一)
- Python入门(一):爬虫基本结构&简单实例
- [mooc]python网络爬虫与信息提取(实例一)
- 爬虫爬虫爬虫(一)
- python 网络爬虫入门(一)———第一个python爬虫实例
- python 网络爬虫入门(一)———第一个python爬虫实例
- 爬虫一
- 爬虫入门:urllib爬虫实例
- java多线程爬虫实例
- java多线程爬虫实例
- python爬虫实例
- Python 爬虫入门实例
- python爬虫小实例
- 算法:选择排序
- JPA EntityManager详解
- Atlassian JIRA 授权许可证机制分析
- 哈哈日语 别人对你说了“斯密马森”,该怎么回?
- 解决windows下tomcat端口被占用
- 爬虫实例(一)
- [LWIP学习]--pbuf_realloc函数分析
- Linux下用phpstudy安装mysql,设置忽略表名大小写
- 牛客网-剑指offer-二维数组查找
- 使用Okhttp框架访问接口数据,通过RecyclerView进行数据展示
- asp.net 数字不够位数,自动补0方法
- Openstack(Ocata)Ironic安装配置手册
- OpenGL超级宝典笔记——深度纹理和阴影
- HDU 6082 度度熊与邪恶大魔王(01背包)