Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战

网络爬虫的“盗亦有道” 和Requests库网络爬取实战

  • 学习笔记手札及单元小结
  • 网络爬虫的“盗亦有道”
    • 网络爬虫的限制
    • Robots协议
    • Robots协议的遵守方式
      • Robots协议的使用
  • Requests库网络爬取实战
    • 实例1:京东商品页面的爬取
    • 实例2:亚马逊商品页面的爬取
    • 实例3:百度/360搜索关键词提交
    • 实例4:网络图片的爬取和存储
    • 实例5:IP地址归属地的自动查询

和Requests库网络爬取实战)

学习笔记手札及单元小结

Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第1张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第2张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第3张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第4张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第5张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第6张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第7张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第8张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第9张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第10张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第11张图片
Python网络爬虫之网络爬虫的“盗亦有道”和Requests库网络爬取实战学习笔记手札及代码实战_第12张图片

网络爬虫的“盗亦有道”

网络爬虫的限制

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

Robots协议

1.作用:
网站告知网络爬虫哪些页面可以抓取,哪些不行
2.形式:
在网站根目录下的robots.txt文件

案例:京东的Robots协议

https://www.jd.com/robots.txt
Robots协议基本语法:
#注释,*代表所有,/代表根目录

User-agent: * 
Disallow: /?* 
Disallow: /pop/*.html 
Disallow: /pinpai/*.html?* 
User-agent: EtaoSpider 
Disallow: / 
User-agent: HuihuiSpider 
Disallow: / 
User-agent: GwdangSpider 
Disallow: / 
User-agent: WochachaSpider 
Disallow: /

https://news.sina.com.cn/robots.txt

User-agent: *
Disallow: /wap/
Disallow: /iframe/
Disallow: /temp/

https://www.qq.com/robots.txt

User-agent: *
Disallow:  
Sitemap: http://www.qq.com/sitemap_index.xml

https://news.qq.com/robots.txt

User-agent: *
Disallow:  
Sitemap: http://www.qq.com/sitemap_index.xml
Sitemap: http://news.qq.com/topic_sitemap.xml

https://www.moe.edu.cn/robots.txt
无robots协议
感兴趣的小伙伴可以自己去尝试一下

Robots协议的遵守方式

Robots协议的使用

1.网络爬虫:
自动或人工识别robots.txt,再进行内容爬取
2.约束性:
Robots协议是建议但非约束性,网络爬虫可以不遵守,但存在法律风险。

Requests库网络爬取实战

实例1:京东商品页面的爬取

全代码:

import requests
url = "https://item.jd.com/2967929.html"
try:
    r = requests.get(url)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    print(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 "", 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    \n    \n\n
                    
                    

你可能感兴趣的:(笔记,金融工程考研备忘录,网络,搜索引擎,百度,python,https)