Learn python the ninth day

原文链接: http://www.cnblogs.com/feiyufei/p/11066377.html



今日内容: 1 Scrapy爬虫框架
2 微信机器人
'''
一、Scrapy爬虫框架 发送请求 ---> 获取响应数据 ---> 解析数据 ---> 保存数据 #1 Scarpy框架介绍:

1) 引擎(EGINE)
引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件。有关详细信息,请参见上面的数据流部分。

2) 调度器(SCHEDULER)
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址

3) 下载器(DOWLOADER) 用于下载网页内容, 并将网页内容返回给EGINE,下载器是建立在twisted这个高效的异步模型上的
4) 爬虫(SPIDERS)
SPIDERS是开发人员自定义的类,用来解析responses,并且提取items,或者发送新的请求

5) 项目管道(ITEM PIPLINES)
在items被提取后负责处理它们,主要包括清理、验证、持久化(比如存到数据库)等操作
下载器中间件(Downloader Middlewares)位于Scrapy引擎和下载器之间,主要用来处理从EGINE传到DOWLOADER的请求request,已经从DOWNLOADER传到EGINE的响应response,
你可用该中间件做以下几件事:
  (1) process a request just before it is sent to the Downloader (i.e. right before Scrapy sends the request to the website);
  (2) change received response before passing it to a spider;
  (3) send a new Request instead of passing received response to a spider;
  (4) pass response to a spider without fetching a web page;
  (5) silently drop some requests.

6、爬虫中间件(Spider Middlewares)
位于EGINE和SPIDERS之间,主要工作是处理SPIDERS的输入(即responses)和输出(即requests)

#2 Scarpy安装

1) pip3 install wheel
2) pip3 install lxml
3) pip3 install pyopenssl
4) pip3 install pypiwin32
5) 安装twisted框架
下载twisted: http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted

安装下载好的twisted:
pip3 install 下载目录\Twisted-17.9.0-cp36-cp36m-win_amd64.whl

6) pip3 install scrapy

 #  Scarpy使用
1) 进入终端cmd
- scrapy
C:\Users\administortra>scrapy
Scrapy 1.6.0 - no active project

2) 创建scrapy项目
1.创建一个文件夹,专门用于存放scrapy项目
- D:\Scrapy_prject
2.cmd终端输入命令
scrapy startproject Spider_Project( 项目名)
- 会在 D:\Scrapy_prject文件夹下会生成一个文件
Spider_Project : Scrapy项目文件

3.创建爬虫程序
cd Spider_Project # 切换到scrapy项目目录下
# 爬虫程序名称 目标网站域名
scrapy genspider baidu www.baidu.com # 创建爬虫程序

#3、启动scrapy项目,执行爬虫程序

# 1) 找到爬虫程序文件进行执行
scrapy runspider只能执行某个 爬虫程序.py
# 切换到爬虫程序执行文件目录下
- cd D:\Scrapy_prject\Spider_Project\Spider_Project\spiders
- scrapy runspider baidu.py

# 2) 根据爬虫名称找到相应的爬虫程序执行
scrapy crawl 爬虫程序名称
# 切换到项目目录下
- cd D:\Scrapy_prject\Spider_Project
- scrapy crawl baidu
'''

# 二、微信机器人

# 安装 wxpy 支持 Python 3.4-3.6,以及 2.7 版本

pip3 install -U wxpy

pip3 install pillow

pip3 install pyecharts

1.扫码登录

# 1

# from wxpy import Bot
# print(222)
# bot=Bot(cache_path=True)
# print(111)

2. 微信好友男女比例

 1 # 2
 2 
 3 from wxpy import Bot
 4 from pyecharts import Pie
 5 import webbrowser
 6 
 7 # 实例化一个微信机器人对象
 8 bot = Bot()
 9 
10 # 获取到微信的所有好友
11 friends = bot.friends()
12 
13 # 设定男性\女性\位置性别好友名称
14 attr = ['男性朋友', '女性朋友', '不明属性']
15 
16 # 初始化对应好友数量
17 value = [0, 0, 0]
18 
19 # 遍历所有的好友,判断这个好友是男性还是女性
20 for friend in friends:
21     if friend.sex == 1:
22         value[0] += 1
23     elif friend.sex == 2:
24         value[1] += 1
25     else:
26         value[2] += 1
27 
28 # 实例化一个饼状图对象
29 pie = Pie('好友们!')
30 
31 # 图表名称str,属性名称list,属性所对应的值list,is_label_show是否现在标签
32 pie.add('', attr, value, is_label_show=True)
33 
34 # 生成一个html文件
35 pie.render('friends.html')
36 
37 # 打开html文件
38 webbrowser.open('friends.html')
View Code

3. 微信好友所在地域分布

安装:

pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
pip3 install echarts-china-counties-pypkg
pip3 install echarts-china-misc-pypkg
 1 # 3
 2 from wxpy import *
 3 from pyecharts import Map
 4 import webbrowser
 5 bot=Bot(cache_path=True)
 6 
 7 friends=bot.friends()
 8 
 9 
10 area_dic={}#定义一个字典,用来存放省市以及省市人数
11 for friend in friends:
12     if friend.province not in area_dic:
13         area_dic[friend.province]=1
14     else:
15         area_dic[friend.province]+=1
16 
17 attr = area_dic.keys()
18 value = area_dic.values()
19 
20 
21 
22 map = Map("微信朋友所在地域分布", width=1200, height=600)
23 map.add(
24     "好友地域分布",
25     attr,
26     value,
27     maptype='china',
28     is_visualmap=True, #结合体VisualMap
29 
30 )
31 #is_visualmap -> bool 是否使用视觉映射组件
32 #
33 map.render('area.html')
34 
35 
36 webbrowser.open("area.html")
View Code

4 聊天机器人

 http://www.tuling123.com/
 1 import jason
 2 import requests
 3 from wxpy import *
 4 bot = Bot(cache_path=True)
 5 
 6 # 调用图灵机器人API,发送消息并获得机器人的回复
 7 def auto_reply(text):
 8     url = "http://www.tuling123.com/openapi/api"
 9     api_key = "9df516a74fc443769b233b01e8536a42"
10     payload = {
11         "key": api_key,
12         "info": text,
13     }
14     r = requests.post(url, data=json.dumps(payload))
15     result = json.loads(r.content)
16     return "[来自智能机器人] " + result["text"]
17 
18 
19 @bot.register()
20 def forward_message(msg):
21     return auto_reply(msg.text)
22 
23 embed()
View Code
为期两周的python已结束,十分感谢tank老师的指导。
 
 

转载于:https://www.cnblogs.com/feiyufei/p/11066377.html

猜你喜欢

转载自blog.csdn.net/weixin_30706507/article/details/94842794