网络爬虫的盗之有道


'''
一 爬虫网络的尺寸:
1 小规模,数据量小,爬取速度不敏感;利用Request库爬取网页和玩转网页
2 中规模:数据规模较大,爬取速度敏感;可以利用scrapy库爬取网站或者爬取系列网站
3 大规模,搜索引擎爬取速度关键,主要是通过定制开发,而不是某一个库就可以完成的,可以用于爬取全网
二 爬虫网络的骚扰:
受限于编写水平和目的,网络爬虫将会为web服务器带来巨大的资源开销

三 网络爬虫具有的风险:
网络爬虫的法律风险 :
1 服务器上的数据有产权归属
2 网络爬虫获取数据后牟利将带来法律风险
网络爬虫泄漏隐私
网络爬虫可能具备突破简单访问控制的能力,获得被保护数据从而泄漏个人隐私。

爬虫网络的限制:
来源审查:判断user-agent进行限制,检查来访HTTP协议头的User-Agent域,
只响应浏览器或友好爬虫的访问。
发布公告:Robots协议:告知所有的爬虫网站的爬取策略,要求爬虫遵守。

Robots协议的使用:
网络爬虫:自动或人工识别robots.txt,再进行内容爬取
约束性:robots协议是建议但非约束性,网络爬虫可以不遵守,但存在法律风险。
'''
#爬取京东某件商品的信息
import requests
url = "https://item.jd.com/2967929.html"
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_endcoding
print (r.text)
except:
print("crawl fail")
print r.status_code
print r.headers
#输出结果为:
#>>> crawl fail
#>>> 200
#>>> {'Via': 'BJ-H-NX-110(HIT), http/1.1 GZ-CM-1-JCS-116 ( [cSsSfU])', 'ser': '3.85', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked', 'Age': '0', 'Expires': 'Sat, 09 Sep 2017 01:47:54 GMT', 'Vary': 'Accept-Encoding', 'hh': '1-1', 'Server': 'JDWS/2.0', 'Last-Modified': 'Sat, 09 Sep 2017 01:46:55 GMT', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=60', 'Date': 'Sat, 09 Sep 2017 01:46:54 GMT', 'Content-Type': 'text/html; charset=gbk'}


#通过百度的关键词搜索
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 "crawl fail"
#输出结果为:
#>>>http://www.baidu.com/s?wd=Python
#>>>353592

import requests
import os
url = "http://image.nationalgeographic.com.cn/2017/0211/20170211061910157.jpg"
root ="E://beifeng//"
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("crawl fail")
#输出内容为:
#>>> 文件保存成功
原文地址:https://www.cnblogs.com/papapython/p/7494479.html