python爬虫的4个实例

416次阅读
没有评论
python爬虫的4个实例

文章目录

        • 1、京东商品页面的爬取
        • 2、亚马逊商品页面的爬取

可以先看 网络爬虫基础知识,然后结合下面的实例学习爬虫的常用方法。

1、京东商品页面的爬取

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:UsersAdminAnaconda3python.exe "E:/2019/May 1/spider JD.py" <!DOCTYPE HTML> <html lang="zh-CN"> <head> <!– shouji –> <meta http-equiv="Content-Type" content="text/html; charset=gbk" /> <title>【全棉时代棉柔巾】全棉时代 居家洁面纯棉柔巾 纯棉抽纸巾湿水可用 洗脸巾擦脸巾20cm*20cm 6包/提【行情 报价 价格 评测】-京东</title> <meta name="keywords" content="PurCotton棉柔巾,全棉时代棉柔巾,全棉时代棉柔巾报价,PurCotton棉柔巾报价"/> <meta name="description" content="【全棉时代棉柔巾】京东JD.COM提供全棉时代棉柔巾正品行货,并包括PurCotton棉柔巾网购指南,以及全棉时代棉柔巾图片、棉柔巾参数、棉柔巾评论、棉柔巾心得、棉柔巾技巧等信息,网购全棉时代棉柔巾上京东,放心又轻松" /> <meta name="format-detection" content="telephone=no"> <meta http-equiv="mobile-agent" content="format=xhtml; url=//item.m.jd.com/product/3112072.html"> <meta http-equiv="mobile-agent" content="format=html5; url=//item.m.jd.com/product/3112072.html"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <link rel="canonical" href="//item.jd.com/3112072.html"/> <link rel="dns-prefetch" href="//misc.360buyimg.com"/> <link rel="dns-prefetch" href="//static.360buyimg.com"/> <link rel="dns-prefetch" href="

Process finished with exit code 0

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

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" 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:UsersAdminAnaconda3python.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<a.ec&&d.uex("at");a.pec=a.ec},1E4)}};l.skipTrace=1;h.skipTrace=1;h.isStub=1;d.ueLogError=h;d.ue_err=a;e.onerror=l})(ue_csm,window);

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("爬取失败")

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-28发表,共计4137字。
新手QQ群:570568346,欢迎进群讨论 Python51学习