Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation and remove all type from history page #89

Merged
merged 10 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[![PyPI](https://img.shields.io/pypi/wheel/wechatsogou.svg)](https://github.com/Chyroc/WechatSogou)
[![py27,py35,py36](https://img.shields.io/pypi/pyversions/wechatsogou.svg)](https://github.com/Chyroc/WechatSogou)
[![PyPI](https://img.shields.io/pypi/l/wechatsogou.svg)](https://github.com/Chyroc/WechatSogou)
[![codebeat badge](https://codebeat.co/badges/45119f66-fca7-4f0e-bd0e-35c0dd60a11e)](https://codebeat.co/projects/github-com-chyroc-wechatsogou-master)

# 项目简介
基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫
Expand Down
12 changes: 10 additions & 2 deletions test/test_api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io
import os
import unittest
from nose.tools import assert_raises, assert_equal, assert_in, assert_not_equal
from nose.tools import assert_raises, assert_equal, assert_in, assert_not_equal, assert_not_in

import httpretty
from hypothesis import given, strategies as st
Expand All @@ -32,7 +32,15 @@ def test_gen_search_article_url_page(self, page):

@given(st.integers(min_value=-50, max_value=50), st.dates(), st.dates())
def test_gen_search_article_url_timesn(self, timesn, ft, et):
if timesn in [1, 2, 3, 4]:
if timesn == 0:
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn)
assert_in('type=2&page=1&ie=utf8&query=', url)
assert_not_in('ft=&et=', url)

url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn, ft=str(ft))
assert_in('type=2&page=1&ie=utf8&query=', url)
assert_not_in('ft=&et=', url)
elif timesn in [1, 2, 3, 4]:
url = WechatSogouRequest._gen_search_article_url(gaokao_keyword, timesn=timesn)
assert_in('tsn={}&ft=&et='.format(timesn), url)

Expand Down
2 changes: 1 addition & 1 deletion test/test_api_structuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def test_get_article_by_search(self):
'黑龙江省2017年普通高考成绩即将发布 我省今年高考网上评卷工作现已结束,经过成绩核查、成绩校验等多个环节后,我省高考成绩...',
'2017年高考录取工作开始了,各省区市高考录取工作何时进行?为了方便考生和家长及时了解,小编为大家作了最新最全的梳理.(图...',
'各地高考成绩已陆续公布,在本公众号回复“高考查分”即可查询!~长按二维码即可关注本车~自昨天开始,全国各省份陆续公布...',
'导语高考成绩和批次线已经出来了,想必同学们已经开始进入另一重要环节——志愿填报.你是不是在为选专业而纠结痛苦?不怕!...', ],
'导语高考成绩和批次线已经出来了,想必同学们已经开始进入另一重要环节——志愿填报.你是不是在为选专业而纠结痛苦?不怕!...'],
abstracts)

# gzh
Expand Down
110 changes: 77 additions & 33 deletions wechatsogou/refactor_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,37 @@ class WechatSogouRequest(object):
TYPE_ALL = 'all'

@staticmethod
def _gen_search_article_url(keyword, page=1, timesn=None, article_type=TYPE_ALL, wxid=None, usip=None, ft=None,
def _gen_search_article_url(keyword, page=1, timesn=0, article_type=TYPE_ALL, wxid=None, usip=None, ft=None,
et=None):
""" 拼接搜索 文章 URL

:param keyword: 搜索文字
:param page: 页数
:param timesn: 时间 1一天 / 2一周 / 3一月 / 4一年 / 5自定
:param ft: 当 tsn 是 5 时,本参数代表时间,如: 2017-07-01
:param et: 当 tsn 是 5 时,本参数代表时间,如: 2017-07-15
:param artilce_type: 含有内容的类型: TYPE_IMAGE 有图 / TYPE_VIDEO 有视频 / TYPE_RICH 有图和视频 / TYPE_ALL 啥都有
:param wxid:
:param usip: wxid usip 联合起来就是账号内搜索
:return:
"""拼接搜索 文章 URL

Parameters
----------
keyword : str or unicode
搜索文字
page : int, optional
页数 the default is 1
timesn : {0, 1, 2, 3, 4, 5}
时间 0 没有限制 / 1一天 / 2一周 / 3一月 / 4一年 / 5自定
the default is 0
article_type : {'image', 'video', 'rich', 'all'}
含有内容的类型 TYPE_IMAGE 有图 / TYPE_VIDEO 有视频 / TYPE_RICH 有图和视频 / TYPE_ALL 啥都有
wxid : None
wxid usip 联合起来就是账号内搜索
usip : None
wxid usip 联合起来就是账号内搜索
ft, et : datetime.date
当 tsn 是 5 时,ft 代表开始时间,如: 2017-07-01
当 tsn 是 5 时,et 代表结束时间,如: 2017-07-15

Returns
-------
str
search_article_url
"""

assert isinstance(page, int) and page > 0
assert timesn in [1, 2, 3, 4, 5, None]
assert timesn in [0, 1, 2, 3, 4, 5]

if timesn == 5:
assert isinstance(ft, datetime.date)
Expand All @@ -59,7 +73,7 @@ def _gen_search_article_url(keyword, page=1, timesn=None, article_type=TYPE_ALL,
qsDict['page'] = page
qsDict['ie'] = 'utf8'
qsDict['query'] = keyword
if timesn is not None:
if timesn != 0:
qsDict['tsn'] = timesn
qsDict['ft'] = str(ft)
qsDict['et'] = str(et)
Expand All @@ -75,13 +89,20 @@ def _gen_search_article_url(keyword, page=1, timesn=None, article_type=TYPE_ALL,

@staticmethod
def _gen_search_gzh_url(keyword, page=1):
""" 拼接搜索 公众号 URL

:param keyword: 搜索文字
:param page: 页数
:return:
"""拼接搜索 公众号 URL

Parameters
----------
keyword : str or unicode
搜索文字
page : int, optional
页数 the default is 1

Returns
-------
str
search_gzh_url
"""

assert isinstance(page, int) and page > 0

qsDict = OrderedDict()
Expand All @@ -93,19 +114,34 @@ def _gen_search_gzh_url(keyword, page=1):
return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qsDict))

@staticmethod
def _search_article(keyword, page=1, timesn=None, article_type=None, wxid=None, usip=None, ft=None, et=None):
def _search_article(keyword, page=1, timesn=0, article_type=None, wxid=None, usip=None, ft=None, et=None):
"""搜索 文章 获取文本

:param keyword: 关键词
:param page: 页数
:param timesn: 时间 1一天 / 2一周 / 3一月 / 4一年 / 5自定
:param article_type: 含有内容的类型: TYPE_IMAGE 有图 / TYPE_VIDEO 有视频 / TYPE_RICH 有图和视频 / TYPE_ALL 啥都有
:param wxid:
:param usip: wxid usip 联合起来就是账号内搜索
:param ft: 当 tsn 是 5 时,本参数代表时间,如: 2017-07-01
:param et: 当 tsn 是 5 时,本参数代表时间,如: 2017-07-15
:return:
Parameters
----------
keyword : str or unicode
搜索文字
page : int, optional
页数 the default is 1
timesn : {0, 1, 2, 3, 4, 5}
时间 0 没有限制 / 1一天 / 2一周 / 3一月 / 4一年 / 5自定
the default is 0
article_type : {'image', 'video', 'rich', 'all'}
含有内容的类型 TYPE_IMAGE 有图 / TYPE_VIDEO 有视频 / TYPE_RICH 有图和视频 / TYPE_ALL 啥都有
wxid : None
wxid usip 联合起来就是账号内搜索
usip : None
wxid usip 联合起来就是账号内搜索
ft, et : datetime.date
当 tsn 是 5 时,ft 代表开始时间,如: 2017-07-01
当 tsn 是 5 时,et 代表结束时间,如: 2017-07-15

Returns
-------
requests
return of requests
"""

url = WechatSogouRequest._gen_search_article_url(keyword, page, timesn, article_type, wxid, usip, ft, et)
r = requests.get(url)
if not r.ok:
Expand All @@ -117,9 +153,17 @@ def _search_article(keyword, page=1, timesn=None, article_type=None, wxid=None,
def _search_gzh(keyword, page=1):
"""搜索 公众号 获取文本

:param keyword: 公众号关键词 / 微信号
:param page: 页数 1-n
:return:
Parameters
----------
keyword : str or unicode
搜索文字
page : int, optional
页数 the default is 1

Returns
-------
requests
return of requests
"""
url = WechatSogouRequest._gen_search_gzh_url(keyword, page)
r = requests.get(url)
Expand Down
59 changes: 47 additions & 12 deletions wechatsogou/refactor_structuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ class WechatSogouStructuring(object):
def get_gzh_by_search(text):
"""从搜索公众号获得的文本 提取公众号信息

:param text: 搜索公众号获得的文本
:return: list of dict:
Parameters
----------
text : str or unicode
搜索公众号获得的文本

Returns
-------
list of dict
{
'url': '',
'img': '',
Expand Down Expand Up @@ -58,8 +64,14 @@ def get_gzh_by_search(text):
def get_article_by_search(text):
"""从搜索文章获得的文本 提取章列表信息

:param text: 搜索文章获得的文本
:return: list of dict:
Parameters
----------
text : str or unicode
搜索文章获得的文本

Returns
-------
list of dict
{
'article': {
'title': '',
Expand Down Expand Up @@ -143,8 +155,14 @@ def get_article_by_search(text):
def get_gzh_info_by_history(text):
"""从 历史消息页的文本 提取公众号信息

:param text: 历史消息页的文本
:return: dict:
Parameters
----------
text : str or unicode
历史消息页的文本

Returns
-------
dict
{
'name': '',
'wechat_id': '',
Expand Down Expand Up @@ -174,10 +192,21 @@ def get_gzh_info_by_history(text):
def get_article_by_history_json(text, article_json=None, **kwargs):
"""从 历史消息页的文本 提取文章列表信息

:param text: 历史消息页的文本
:param article_json: 历史消息页的文本 提取出来的文章json
:param kwargs: ??
:return: (注:目前只能获得49 文章 类型 的数据了)
Parameters
----------
text : str or unicode
历史消息页的文本
article_json : dict
历史消息页的文本 提取出来的文章json dict
kwargs
??

Returns
-------
list of dict
{
??
}
"""
# TODO 加上返回的数据的文档
if article_json is None:
Expand Down Expand Up @@ -269,8 +298,14 @@ def get_article_by_history_json(text, article_json=None, **kwargs):
def get_gzh_info_and_article_by_history(text):
"""从 历史消息页的文本 提取公众号信息 和 文章列表信息

:param text: 历史消息页的文本
:return: dict:
Parameters
----------
text : str or unicode
历史消息页的文本

Returns
-------
dict
{
'gzh_info': WechatSogouStructuring.get_gzh_info_by_history 函数返回值,
'article': WechatSogouStructuring.get_article_by_history_json 函数返回值
Expand Down