python 6.21

一.今日内容:

1.scrapy爬虫框架的使用:

一 Scrapy爬虫框架
发送请求 ---> 获取响应数据 ---> 解析数据 ---> 保存数据

** 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)

** 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项目,执行爬虫程序

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

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

2.微信机器人:

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

pip3 install -U wxpy

安装 pillow模块

pip3 install pillow

安装 pyecharts模块

pip3 install pyecharts

$ 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

from wxpy import *
bot = Bot()

bot = Bot(cache_path=True) # 必须先登录过一次以后才可以使用缓存

from wxpy import Bot
from pyecharts import Pie
import webbrowser

# 实例化一个微信机器人对象
bot = Bot()

# 获取到微信的所有好友
friends = bot.friends()

# 设定男性\女性\位置性别好友名称
attr = [‘男朋友‘, ‘女朋友‘, ‘未知‘]

# 初始化对应好友数量
value = [0, 0, 0]

# 遍历所有的好友,判断这个好友是男性还是女性
for friend in friends:
    if friend.sex == 1:
        value[0] += 1
    elif friend.sex == 2:
        value[1] += 1
    else:
        value[2] += 1

# 实例化一个饼状图对象
pie = Pie(‘hao的好友们!‘)

# 图表名称str,属性名称list,属性所对应的值list,is_label_show是否现在标签
pie.add(‘‘, attr, value, is_label_show=True)

# 生成一个html文件
pie.render(‘friends.html‘)

# 打开html文件
webbrowser.open(‘friends.html‘)
from wxpy import *
from pyecharts import Map
import webbrowser
bot=Bot(cache_path=True)

friends=bot.friends()

area_dic={}#定义一个字典,用来存放省市以及省市人数
for friend in friends:
    if friend.province not in area_dic:
        area_dic[friend.province]=1
    else:
        area_dic[friend.province]+=1

attr = area_dic.keys()
value = area_dic.values()

map = Map("好朋友们的地域分布", width=1200, height=600)
map.add(
    "好友地域分布",
    attr,
    value,
    maptype=‘china‘,
    is_visualmap=True, #结合体VisualMap

)
#is_visualmap -> bool 是否使用视觉映射组件
#
map.render(‘area.html‘)

webbrowser.open("area.html")

bot.file_helper.send(‘lqz say hello‘)

from wxpy import *
bot=Bot(cache_path=True)

@bot.register()
def recv_send_msg(recv_msg):
    print(‘收到的消息:‘,recv_msg.text) # recv_msg.text取得文本
    return ‘自动回复:%s‘ %recv_msg.text

# 进入Python命令行,让程序保持运行
embed()
from wxpy import *
bot=Bot(cache_path=True)

girl_friend=bot.search(‘女朋友的备注名称‘)[0]
print(girl_friend)

@bot.register() # 接收从指定好友发来的消息,发送者即recv_msg.sender为指定好友girl_friend
def recv_send_msg(recv_msg):
    print(‘收到的消息:‘,recv_msg.text) # recv_msg.text取得文本
    if recv_msg.sender == girl_friend:
        recv_msg.forward(bot.file_helper,prefix=‘老婆留言: ‘) #在文件传输助手里留一份,方便自己忙完了回头查看
        ms=‘老婆最美丽,我对老婆的爱如滔滔江水,连绵不绝‘
        print(‘>>>给老婆回复的:‘, ms)
        return  ms#给老婆回一份

embed()

from wxpy import *
bot=Bot(cache_path=True)

company_group=bot.groups().search(‘群名字‘)[0]

boss=company_group.search(‘老板名字‘)[0]

@bot.register(chats=company_group) #接收从指定群发来的消息,发送者即recv_msg.sender为组
def recv_send_msg(recv_msg):
    print(‘收到的消息:‘,recv_msg.text)
    if recv_msg.member == boss:
        #这里不用recv_msg.render 因为render是群的名字
        recv_msg.forward(bot.file_helper,prefix=‘老板发言: ‘)
        return ‘老板说的好有道理,深受启发‘

embed()
import json
import requests
from wxpy import *
bot = Bot(cache_path=True)

# 调用图灵机器人API,发送消息并获得机器人的回复
def auto_reply(text):
    url = "http://www.tuling123.com/openapi/api"
    api_key = "9df516a74fc443769b233b01e8536a42"
    payload = {
        "key": api_key,
        "info": text,
    }
    r = requests.post(url, data=json.dumps(payload))
    result = json.loads(r.content)
    return "[来自智能机器人] " + result["text"]

@bot.register()
def forward_message(msg):
    return auto_reply(msg.text)

embed()

import json
import requests
from wxpy import *
bot = Bot(cache_path=False)

group=bot.groups().search(‘群名字‘)[0]
print(group)

# 调用图灵机器人API,发送消息并获得机器人的回复
def auto_reply(text):
    url = "http://www.tuling123.com/openapi/api"
    api_key = "9d602fe417464cd18beb2083d064bee6"
    payload = {
        "key": api_key,
        "info": text,
    }
    r = requests.post(url, data=json.dumps(payload))
    result = json.loads(r.content)
    return "[来自智能机器人] " + result["text"]

@bot.register(chats=group)
def forward_message(msg):
    return auto_reply(msg.text)

embed()

import requests
from wxpy import *
bot = Bot( cache_path=True)

girl_friend=bot.search(‘名字r‘)[0]

# 调用图灵机器人API,发送消息并获得机器人的回复
def auto_reply(text):
    url = "http://www.tuling123.com/openapi/api"
    api_key = "申请图灵机器人获取key值放到这里"
    payload = {
        "key": api_key,
        "info": text,
    }
    r = requests.post(url, data=json.dumps(payload))
    result = json.loads(r.content)
    return "[微信测试,请忽略] " + result["text"]

@bot.register()
def forward_message(msg):
    if msg.sender == girl_friend:
        return auto_reply(msg.text)

embed()

原文地址:https://www.cnblogs.com/zhangtingyu/p/11067306.html

时间: 2024-08-04 10:54:54

python 6.21的相关文章

Python 20.21. cookielib模块翻译

Python 20.21.用于http客户端的处理的模块 By 白熊花田(http://blog.csdn.net/whiterbear) 转载请注明出处,谢谢. 原文链接:https://docs.python.org/2/library/cookielib.html 标注: cookielib模块已经在python3中改名为http.cookiejar了.2to3这个工具能够自动地在你将代码由python2.x转为python3.x帮你更正源码. 简介: cookielib模块中定义了几个处

python自动化21期day8

一.异常处理 # try excpet try: choice = int(input(">>>")) print(choice) except ValueError: print("您输入的不是数字") # 万能异常 # 所有的异常处理都用万能异常好不好? # 具体的异常处理+万能异常: # 能够提前预料到的异常都应该用具体的异常去处理,剩下其他的异常用万能异常控制 # 万能异常应该写在最后 try: choice = int(input(&qu

欧拉计划(python) problem 21

Amicable numbers Problem 21 Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numb

2015/10/9 Python基础(21):可调用和可执行对象

在Python中有多种运行外部程序的方法,比如,运行操作系统命令或另外的Python脚本,或执行一个磁盘上的文件,或通过网络来运行文件.这完全取决于想要干什么.特定的环境包括: 在当前脚本继续运行 创建和管理子进程 执行外部命令或程序 执行需要输入的命令 通过网络来调用命令 执行命令来创建需要处理的输出 执行其他的Python脚本 执行一系列动态生成的Python语句 导入Python模块 Python中,内建和外部模块都可以提供上述各种功能.程序员得根据实现的需要,从这些模块中选择合适的处理方

Python学习-21.Python的代码注释

在Python中有两种注释,一种是普通注释,另一种是文档注释. 普通注释是使用#开头 1 print('output something') # here is comment 而Python中多行注释也是使用# 1 # comment 1 2 # comment 2 3 # comment 3 而文档注释则是使用英文的三个单引号 1 def Print(msg): 2 '''输出字符串 3 msg: 字符串内容 4 ''' 5 print(msg) 而在我们使用这个Print函数的时候VS的智

Python 练习 21

#!/usr/bin/python # -*- coding: UTF-8 -*- h = 0 leap = 1 from math import sqrt from sys import stdout for m in range(101,201): k = int(sqrt(m + 1)) for i in range(2,k + 1): if m % i == 0: leap = 0 break if leap == 1: print '%-4d' % m h += 1 if h % 10

python自动化21期day7

一.面向对象 1.封装 # 广义上的封装 :把变量和函数都放在类中 # 狭义上的封装 :把一些变量 或者 方法 隐藏起来,不对外公开 # 公有的 : # 私有的 : __名字 # 静态属性 . 对象属性. 方法(动态属性) 前面加上双下划綫都会变成私有的 # 私有的特点就是只能在类的内部调用,不能在类的外部使用 # 私有的变量 :在类的内部 如果使用__变量的形式会发生变形,python会自动的为你加上_类名 class Person: __country = '中国' # 私有静态属性 def

案例:python玩21点

两个玩家,游戏开始先输入名字 用字典保存每个玩家信息:姓名,获胜次数 电脑随机产生2个数,每个玩家轮流猜1个数,与电脑随机两个数求和,最接近21的获胜 每轮结束显示玩家信息 按q退出游戏 import random user1 = input('user1name:') user2 = input('user2name:') user_info = {user1: {'win':0}, user2: {'win':0} } while True: computer1_num = random.r

python爬虫21 | 对于b站这样的滑动验证码,不好意思,照样自动识别

今天 要来说说滑动验证码了 大家应该都很熟悉 点击滑块然后移动到图片缺口进行验证 现在越来越多的网站使用这样的验证方式 为的是增加验证码识别的难度 那么 对于这种验证码 应该怎么破呢 接下来就是 学习 python 的正确姿势 打开 b 站的登录页面 https://passport.bilibili.com/login 可以看到登录的时候需要进行滑块验证 按下 F12 进入 Network 看下我们将滑块移到缺口松开之后做了什么提交 可以看到是一个 GET 请求 但是 这请求链接也太特么长了吧