Python爬虫入门-weoffer

爬完了实习信息,接下来做的是将我的程序和洒哥哥之前总的程序合并在一起,下面将学习一些新的方法,主要记录一下itchat,一个开源的微信个人号接口的用法。
分享一个写的非常全非常细致的文章供参考,python实现微信接口(itchat)
本篇博客暂且先学习一下爬虫中用到的功能~~~~

itchat库

itchat是一个开源的微信个人号接口,可以使用该库进行微信网页版中的所有操作,比如:所有好友、添加好友、拉好友群聊、微信机器人等等。

导入itchat库

1
import itchat

登陆

itchat.auto_login()这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实短时间的登录,并不会保留登录的状态,也就是下次登录时还是需要扫描二维码,如果加上hotReload==True,那么就会保留登录的状态,至少在后面的几次登录过程中不会再次扫描二维码,该参数生成一个静态文件itchat.pkl用于存储登录状态。

因此,在程序的主函数,第一行就是:登陆微信网页版,并保留登陆状态

1
itchat.auto_login(hotReload=True)

好友

get_friends

  • itchat.get_friends() 返回完整的好友列表
  • 每个好友为一个字典, 其中第一项为本人的账号信息;
  • 传入 update=True, 将更新好友列表并返回, get_friends(update=True)

search_friends

itchat.search_friends() 好友搜索,有以下四种方式:

  • 仅获取自己的用户信息: itchat.search_friends()
  • 获取特定UserName的用户信息: itchat.search_friends(userName=‘@abcdefg1234567’)
  • 获取任何一项等于name键值的用户:itchat.search_friends(name=’autolife’)
  • 获取分别对应相应键值的用户:itchat.search_friends(wechatAccount=’littlecodersh’)

群聊

  • get_chatrooms : 返回完整的群聊列表.
  • search_chatrooms : 群聊搜索.
  • update_chatroom : 获取群聊用户列表或更新该群聊.

send_msg

send_msg(msg=’Text Message’, toUserName=None),其中的的msg是要发送的文本,toUserName是发送对象, 如果留空, 将发送给自己,返回值为True或者False.

wx方法

下面是爬虫程序中定义的微信方法:

1
2
3
4
5
6
7
8
def wx(name,jobinfo2):
i=itchat.get_chatrooms(update=True)
iRoom = itchat.search_chatrooms(name)
for room in iRoom:
if room['NickName'] == name:
userName = room['UserName']
break
itchat.send_msg(jobinfo2, userName)

缩短网址功能

在程序中,还有一个方法是python3中用五行代码实现缩短网址功能.

先导入包,使用try,保证鲁棒性:

1
2
3
4
5
6
7
8
9
10
import contextlib
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
import sys

下面就是五行代码,方法来自python tips

1
2
3
4
def make_tiny(url):
request_url = ('http://tinyurl.com/api-create.php?' +urlencode({'url': url}))
with contextlib.closing(urlopen(request_url)) as response:
return response.read().decode('utf-8')

python日期和时间

内容来自菜鸟驿站

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。

Python 的 time 模块下有很多函数可以转换常见日期格式。
如函数time.time()用于获取当前时间戳, 如下实例:

1
2
3
4
5
import time
t=time.time()
print(t)

输出:1543149366.2743704

time模块

获取当前时间

从返回浮点数的时间戳方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。

1
2
3
4
5
6
import time
localtime = time.localtime(time.time())
print ("本地时间为 :", localtime)

输出:
本地时间为 : time.struct_time(tm_year=2018, tm_mon=11, tm_mday=25, tm_hour=20, tm_min=38, tm_sec=36, tm_wday=6, tm_yday=329, tm_isdst=0)

获取格式化时间

你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():

1
2
3
4
5
6
import time
localtime = time.asctime( time.localtime(time.time()) )
print ("本地时间为 :", localtime)

输出:
本地时间为 : Sun Nov 25 20:40:04 2018

格式化日期

我们可以使用time模块的 strftime 方法来格式化日期:

1
time.strftime(format[, t])

参数format是格式字符串

datatime模块

datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1;

datetime模块定义了5个类,分别是:

  • 1.datetime.date:表示日期的类
  • 2.datetime.datetime:表示日期时间的类
  • 3.datetime.time:表示时间的类
  • 4.datetime.timedelta:表示时间间隔,即两个时间点的间隔
  • 5.datetime.tzinfo:时区的相关信息

程序中用到的有:
datetime.datetime.now():返回当前系统时间

用python中可以看出:

1
2
3
4
5
6
7
import time
import datetime
t=datetime.datetime.now()
print(t)

输出:
2018-11-25 21:01:56.589646

datetime.datetime.strftime():由日期格式转化为字符串格式

1
2
3
4
5
6
7
import time
import datetime
t=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(t)

输出:
2018-11-25 21:00:57