python搜索引擎网站模板_python爬虫的5个实例

文章目录

1、京东商品页面的爬取

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

3、百度、360搜索关键字提交

1、京东商品页面的爬取

爬虫具体流程可以参照前一篇博客:https://blog.csdn.net/weixin_42515907/article/details/87932185,这里提供的仅仅是爬虫的几个实例演示,前提理论知识需要参照上面博客。

import requests

url = "https://item.jd.com/3112072.html"

try:

r = requests.get(url)

r.raise_for_status()

#查看状态信息,返回的是200,说明返回信息正确并且已经获得该链接相应内容。

r.encoding = r.apparent_encoding

#查看编码格式,这个格式是jbk,说明我们从http的头部分已经可以解析出网站信息。

print(r.text[:1000])

except:

print("爬取失败")

结果:

C:\Users\Admin\Anaconda3\python.exe "E:/2019/May 1/spider JD.py"

【全棉时代棉柔巾】全棉时代 居家洁面纯棉柔巾 纯棉抽纸巾湿水可用 洗脸巾擦脸巾20cm*20cm 6包/提【行情 报价 价格 评测】-京东

try:

r = requests.get(url)

r.raise_for_status()

r.encoding = r.apparent_encoding

print(r.text[:1000])

except:

print("爬取失败")

结果:

和爬取京东商品一样的操作,但是并没有爬取到商品信息,因此我们联想到可能是亚马逊限制了我们的爬虫访问。

限制网络爬虫的方法:

来源审查: 检查来访HTTP协议头的User - Agent域,只响应浏览器或友好爬虫的访问。

发布公告: Robots协议,告知所有爬虫网站的爬取策略,要求爬虫遵守。

import requests

url = "https://www.amazon.cn/dp/B01M8L5Z3Y/ref=sr_1_1?ie=UTF8&qid=1551540666&sr=8-1&keywords=%E6%9E%81%E7%AE%80"

r = requests.get(url)

print(r.status_code)

print(r.encoding)

print(r.request.headers) #Response对象包含request请求,通过r.request.headers查看我们发给亚马逊的request请求的头部倒是是什么内容。

可以看到头部有一个字段是’User-Agent’: ‘python-requests/2.18.4’,说明我们的爬虫告诉亚马逊服务器这次的访问是由一个python的requests库的程序产生的。而亚马逊的来源审查可能不支持这样的访问。

那么我们可以试着更改头部信息,模拟一个浏览器向亚马逊发送请求,操作如下:

import requests

kv = {'User-Agent': 'Mozilla/5.0'} # 是一个标准的浏览器的身份标识的字段

url = "https://www.amazon.cn/dp/B07G7K1Z98/ref=sr_1_3?ie=UTF8&qid=1551539393&sr=8-3&keywords=%E5%B0%8F%E9%B8%9F%E8%80%B3%E6%9C%BA"

r = requests.get(url,headers=kv) #注意这里要加headers,因为headers已经更该过。

print(r.status_code)

print(r.request.headers)

print(r.text[1000:2000])

结果:

C:\Users\Admin\Anaconda3\python.exe "E:/2019/May 1/spider Amazon.py"

200

{'User-Agent': 'Mozilla/5.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}

(function(d,e){function h(f,b){if(!(a.ec>a.mxe)&&f){a.ter.push(f);b=b||{};var c=f.logLevel||b.logLevel;c&&c!==k&&c!==m&&c!==n&&c!==p||a.ec++;c&&c!=k||a.ecf++;b.pageURL=""+(e.location?e.location.href:"");b.logLevel=c;b.attribution=f.attribution||b.attribution;a.erl.push({ex:f,info:b})}}function l(a,b,c,e,g){d.ueLogError({m:a,f:b,l:c,c:""+e,err:g,fromOnError:1,args:arguments},g?{attribution:g.attribution,logLevel:g.logLevel}:void 0);return!1}var k="FATAL",m="ERROR",n="WARN",p="DOWNGRADED",a={ec:0,ecf:0,

pec:0,ts:0,erl:[],ter:[],mxe:50,startTimer:function(){a.ts++;setInterval(function(){d.ue&&a.pec

ue.stub(ue,"event");ue.stub(ue,"onSushiUnload");ue.stub(ue,"onSushiFlush");

var ue_url='/gp/product/B07G7K1Z98/uedata/unsticky/460-8525492-7331338/NoPageType/ntpoffrw',

ue_sid='460-8525492-7331338',

ue_mid='AAHKV2X7AFYLW',

ue_sn='www.amazon.cn',

ue_furl='fls-cn.

Process finished with exit code 0

可见,更改User-Agent属性之后的爬虫可以正常爬取信息。

尝试和修改后的爬虫程序如下:

import requests

url = "https://www.amazon.cn/dp/B07G7K1Z98/ref=sr_1_3?ie=UTF8&qid=1551539393&sr=8-3&keywords=%E5%B0%8F%E9%B8%9F%E8%80%B3%E6%9C%BA"

kv = {'User-Agent': 'Mozilla/5.0'} # 是一个标准的浏览器的身份标识的字段

try:

r = requests.get(url, headers=kv)

r.raise_for_status()

r.encoding = r.apparent_encoding

print(r.text[:1000])

except:

print("爬取失败")

3、百度、360搜索关键字提交

用程序自动地向这两个搜索引擎提交关键词并且获得搜索结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值