爬虫---request+++urllib
网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。
Requests
Python标准库中提供了:urllib、urllib2、httplib等模块以供Http请求,但是,它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。
import urllib2
import json
import cookielib def urllib2_request(url, method="GET", cookie="", headers={}, data=None):
"""
:param url: 要请求的url
:param cookie: 请求方式,GET、POST、DELETE、PUT..
:param cookie: 要传入的cookie,cookie= 'k1=v1;k1=v2'
:param headers: 发送数据时携带的请求头,headers = {'ContentType':'application/json; charset=UTF-8'}
:param data: 要发送的数据GET方式需要传入参数,data={'d1': 'v1'}
:return: 返回元祖,响应的字符串内容 和 cookiejar对象
对于cookiejar对象,可以使用for循环访问:
for item in cookiejar:
print item.name,item.value
"""
if data:
data = json.dumps(data) cookie_jar = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookie_jar)
opener = urllib2.build_opener(handler)
opener.addheaders.append(['Cookie', 'k1=v1;k1=v2'])
request = urllib2.Request(url=url, data=data, headers=headers)
request.get_method = lambda: method response = opener.open(request)
origin = response.read() return origin, cookie_jar # GET
result = urllib2_request('http://127.0.0.1:8001/index/', method="GET") # POST
result = urllib2_request('http://127.0.0.1:8001/index/', method="POST", data= {'k1': 'v1'}) # PUT
result = urllib2_request('http://127.0.0.1:8001/index/', method="PUT", data= {'k1': 'v1'})
封装urllib请求
Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装,从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作。
1、GET请求:
# 1、无参数实例
import requests
ret = requests.get('https://github.com/timeline.json')
print ret.url
print ret.text
# 2、有参数实例
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload)
print ret.url
print ret.text
向 https://github.com/timeline.json 发送一个GET请求,将请求和响应相关均封装在 ret 对象中。
2、POST请求
# 1、基本POST实例
import requests
payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)
print ret.text
# 2、发送请求头和数据实例
import requests
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
ret = requests.post(url, data=json.dumps(payload), headers=headers)
print ret.text
print ret.cookies
向https://api.github.com/some/endpoint发送一个POST请求,将请求和相应相关的内容封装在 ret 对象中。
3、其他的请求
requests.get(url, params=None, **kwargs)
requests.post(url, data=None, json=None, **kwargs)
requests.put(url, data=None, **kwargs)
requests.head(url, **kwargs)
requests.delete(url, **kwargs)
requests.patch(url, data=None, **kwargs)
requests.options(url, **kwargs) # 以上方法均是在此方法的基础上构建
requests.request(method, url, **kwargs)
requests模块已经将常用的Http请求方法为用户封装完成,用户直接调用其提供的相应方法即可,其中方法的所有参数有:
def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`. :param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': ('filename', fileobj)}``) for multipart encoding upload.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How long to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) whether the SSL cert will be verified. A CA_BUNDLE path can also be provided. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response Usage:: >>> import requests
>>> req = requests.request('GET', 'http://httpbin.org/get')
<Response [200]>
""" # By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
return session.request(method=method, url=url, **kwargs)
更多参数
更多requests模块相关的文档见:http://cn.python-requests.org/zh_CN/latest/
“破解”微信公众号
“破解”微信公众号其实就是使用Python代码自动实现【登陆公众号】->【获取观众用户】-> 【向关注用户发送消息】。
1、自动登陆

分析对于Web登陆页面,用户登陆验证时仅做了如下操作:
- 登陆的URL:https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN
- POST的数据为:
{
'username': 用户名,
'pwd': 密码的MD5值,
'imgcode': "",
'f': 'json'
}
注:imgcode是需要提供的验证码,默认无需验证码,只有在多次登陆未成功时,才需要用户提供验证码才能登陆
- POST的请求头的Referer值,微信后台用次来检查是谁发送来的请求
- 请求发送并登陆成功后,获取用户响应的cookie,以后操作其他页面时需要携带此cookie
- 请求发送并登陆成功后,获取用户相应的内容中的token
# -*- coding:utf-8 -*-
import requests
import time
import hashlib def _password(pwd):
ha = hashlib.md5()
ha.update(pwd)
return ha.hexdigest() def login(): login_dict = {
'username': "用户名",
'pwd': _password("密码"),
'imgcode': "",
'f': 'json'
} login_res = requests.post(
url= "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN",
data=login_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}) # 登陆成功之后获取服务器响应的cookie
resp_cookies_dict = login_res.cookies.get_dict()
# 登陆成功后,获取服务器响应的内容
resp_text = login_res.text
# 登陆成功后,获取token
token = re.findall(".*token=(\d+)", resp_text)[0] print resp_text
print token
print resp_cookies_dict login()
登陆代码
登陆成功获取的相应内容如下:
响应内容:
{"base_resp":{"ret":0,"err_msg":"ok"},"redirect_url":"\/cgi-bin\/home?t=home\/index&lang=zh_CN&token=537908795"}
响应cookie:
{'data_bizuin': '3016804678', 'bizuin': '3016804678', 'data_ticket': 'CaoX+QA0ZA9LRZ4YM3zZkvedyCY8mZi0XlLonPwvBGkX0/jY/FZgmGTq6xGuQk4H', 'slave_user': 'gh_5abeaed48d10', 'slave_sid': 'elNLbU1TZHRPWDNXSWdNc2FjckUxalM0Y000amtTamlJOUliSnRnWGRCdjFseV9uQkl5cUpHYkxqaGJNcERtYnM2WjdFT1pQckNwMFNfUW5fUzVZZnFlWGpSRFlVRF9obThtZlBwYnRIVGt6cnNGbUJsNTNIdTlIc2JJU29QM2FPaHZjcTcya0F6UWRhQkhO'}
2、访问其他页面获取用户信息

分析用户管理页面,通过Pyhton代码以Get方式访问此页面,分析响应到的 HTML 代码,从中获取用户信息:
- 获取用户的URL:https://mp.weixin.qq.com/cgi-bin/user_tag?action=get_all_data&lang=zh_CN&token=登陆时获取的token
- 发送GET请求时,需要携带登陆成功后获取的cookie
1
{'data_bizuin':
'3016804678','bizuin':'3016804678',
'data_ticket':
'C4YM3zZ... - 获取当前请求的响应的html代码
- 通过正则表达式获取html中的指定内容(Python的模块Beautiful Soup)
- 获取html中每个用户的 data-fakeid属性,该值是用户的唯一标识,通过它可向用户推送消息
# -*- coding:utf-8 -*-
import requests
import time
import hashlib
import json
import re LOGIN_COOKIES_DICT = {} def _password(pwd):
ha = hashlib.md5()
ha.update(pwd)
return ha.hexdigest() def login(): login_dict = {
'username': "用户名",
'pwd': _password("密码"),
'imgcode': "",
'f': 'json'
} login_res = requests.post(
url= "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN",
data=login_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}) # 登陆成功之后获取服务器响应的cookie
resp_cookies_dict = login_res.cookies.get_dict()
# 登陆成功后,获取服务器响应的内容
resp_text = login_res.text
# 登陆成功后,获取token
token = re.findall(".*token=(\d+)", resp_text)[0] return {'token': token, 'cookies': resp_cookies_dict} def standard_user_list(content):
content = re.sub('\s*', '', content)
content = re.sub('\n*', '', content)
data = re.findall("""cgiData=(.*);seajs""", content)[0]
data = data.strip()
while True:
temp = re.split('({)(\w+)(:)', data, 1)
if len(temp) == 5:
temp[2] = '"' + temp[2] + '"'
data = ''.join(temp)
else:
break while True:
temp = re.split('(,)(\w+)(:)', data, 1)
if len(temp) == 5:
temp[2] = '"' + temp[2] + '"'
data = ''.join(temp)
else:
break data = re.sub('\*\d+', "", data)
ret = json.loads(data)
return ret def get_user_list(): login_dict = login()
LOGIN_COOKIES_DICT.update(login_dict) login_cookie_dict = login_dict['cookies']
res_user_list = requests.get(
url= "https://mp.weixin.qq.com/cgi-bin/user_tag",
params = {"action": "get_all_data", "lang": "zh_CN", "token": login_dict['token']},
cookies = login_cookie_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}
)
user_info = standard_user_list(res_user_list.text)
for item in user_info['user_list']:
print "%s %s " % (item['nick_name'],item['id'],) get_user_list()
代码实现
3、发送消息
分析给用户发送消息的页面,从网络请求中剖析得到发送消息的URL,从而使用Python代码发送消息:
- 发送消息的URL:https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token=登陆时获取的token放在此处&lang=zh_CN
- 从登陆时相应的内容中获取:token和cookie
- 从用户列表中获取某个用户唯一标识: fake_id
- 封装消息,并发送POST请求
send_dict = {
'token': 登陆时获取的token,
'lang': "zh_CN",
'f': 'json',
'ajax': 1,
'random': "0.5322618900912392",
'type': 1,
'content': 要发送的内容,
'tofakeid': 用户列表中获取的用户的ID,
'imgcode': ''
}
# -*- coding:utf-8 -*-
import requests
import time
import hashlib
import json
import re LOGIN_COOKIES_DICT = {} def _password(pwd):
ha = hashlib.md5()
ha.update(pwd)
return ha.hexdigest() def login(): login_dict = {
'username': "用户名",
'pwd': _password("密码"),
'imgcode': "",
'f': 'json'
} login_res = requests.post(
url= "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN",
data=login_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}) # 登陆成功之后获取服务器响应的cookie
resp_cookies_dict = login_res.cookies.get_dict()
# 登陆成功后,获取服务器响应的内容
resp_text = login_res.text
# 登陆成功后,获取token
token = re.findall(".*token=(\d+)", resp_text)[0] return {'token': token, 'cookies': resp_cookies_dict} def standard_user_list(content):
content = re.sub('\s*', '', content)
content = re.sub('\n*', '', content)
data = re.findall("""cgiData=(.*);seajs""", content)[0]
data = data.strip()
while True:
temp = re.split('({)(\w+)(:)', data, 1)
if len(temp) == 5:
temp[2] = '"' + temp[2] + '"'
data = ''.join(temp)
else:
break while True:
temp = re.split('(,)(\w+)(:)', data, 1)
if len(temp) == 5:
temp[2] = '"' + temp[2] + '"'
data = ''.join(temp)
else:
break data = re.sub('\*\d+', "", data)
ret = json.loads(data)
return ret def get_user_list(): login_dict = login()
LOGIN_COOKIES_DICT.update(login_dict) login_cookie_dict = login_dict['cookies']
res_user_list = requests.get(
url= "https://mp.weixin.qq.com/cgi-bin/user_tag",
params = {"action": "get_all_data", "lang": "zh_CN", "token": login_dict['token']},
cookies = login_cookie_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}
)
user_info = standard_user_list(res_user_list.text)
for item in user_info['user_list']:
print "%s %s " % (item['nick_name'],item['id'],) def send_msg(user_fake_id, content='啥也没发'): login_dict = LOGIN_COOKIES_DICT token = login_dict['token']
login_cookie_dict = login_dict['cookies'] send_dict = {
'token': token,
'lang': "zh_CN",
'f': 'json',
'ajax': 1,
'random': "0.5322618900912392",
'type': 1,
'content': content,
'tofakeid': user_fake_id,
'imgcode': ''
} send_url = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token=%s&lang=zh_CN" % (token,)
message_list = requests.post(
url=send_url,
data=send_dict,
cookies=login_cookie_dict,
headers={'Referer': 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'}
) get_user_list()
fake_id = raw_input('请输入用户ID:')
content = raw_input('请输入消息内容:')
send_msg(fake_id, content)
发送消息代码
以上就是“破解”微信公众号的整个过程,通过Python代码实现了自动【登陆微信公众号平台】【获取用户列表】【指定用户发送消息】。
爬虫---request+++urllib的更多相关文章
- 爬虫之urllib.request基础使用(一)
urllib模块 urllib模块简介: urllib提供了一系列用于操作URL的功能.包含urllib.request,urllib.error,urllib.parse,urllib.robotp ...
- 爬虫之urllib包以及request模块和parse模块
urllib简介 简介 Python3中将python2.7的urllib和urllib2两个包合并成了一个urllib库 Python3中,urllib库包含有四个模块: urllib.reques ...
- python 3.x 爬虫基础---Urllib详解
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 前言 爬虫也了解了一段时间了希望在半个月的时间内 ...
- 爬虫之urllib包
urllib简介 简介 Python3中将python2.7的urllib和urllib2两个包合并成了一个urllib库 Python3中,urllib库包含有四个模块: urllib.reques ...
- 爬虫之urllib库
一.urllib库简介 简介 Urllib是Python内置的HTTP请求库.其主要作用就是可以通过代码模拟浏览器发送请求.它包含四个模块: urllib.request :请求模块 urllib.e ...
- 6.python3爬虫之urllib库
# 导入urllib.request import urllib.request # 向指定的url发送请求,并返回服务器响应的类文件对象 response = urllib.request.urlo ...
- python爬虫之urllib库介绍
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
- No module named 'urllib.request'; 'urllib' is not a package
想学爬虫urllib的设置代理服务器,于是把之前跳过没学的urllib捡起来,敲了段简单的代码,如下 import urllib.request url = "http://www.baid ...
- 爬虫中urllib库
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
随机推荐
- cdr创建样式与样式集的方法
样式是一组定义对象属性的格式化属性,如轮廓或填充.例如,要定义轮廓样式,您可以指定轮廓宽度.颜色和线条类型等属性.要定义字符样式,您可以指定字体类型.字体样式和大小.文本颜色和背景色.字符位置.大写等 ...
- 知识积累:DAS NAS SAN
DAS(Direct Acess Storage—直接连接存储)是指将存储设备通过SCSI接口或光纤通道直接连接到一台计算机上.NAS(Network Attached Storage)—网络连接存储 ...
- 如何清除某条SQL的执行计划
如果遇到绑定窥探导致执行计划慢的情况,想要清除某条SQL的执行计划,让它硬解析,找了很久都没有找到直接操作share pool的方法(除非alter system flush shared_pool) ...
- Android AChartEngine 个性化设置
AChartEngine的确是一个强大的图标引擎,但文档写得不是很详细,很多设置只能通过方法名推测和实际尝试,下面是一些自己在实际中遇到的需要设置的选项,常见的那些和通过方法名就能轻松猜到的就不赘述了 ...
- require.js+backbone.js基本使用
一.菜单{ //银行卡基础信息维护 rsId: 'menu.param.cardbin', iconCls: 'icon-double-angle-right', name: menusLang._( ...
- RPC远程过程调用协议
最近学习Hadoop.Hbase.Spark及Storm原理,经常会出现RPC这样的传输术语,为了更好地理解,将知识点详细的整理下吧~ RPC-----它是一种通过网络从远程计算机程序上请求服务,而不 ...
- [Linux] LVM的条带化
一.什么是条带化 当多个进程同时访问一个磁盘时,可能会出现磁盘冲突.磁盘系统对访问次数(每秒的IO操作,IOPS)和数据传输速率(读写速率,TPS)有限制. 当达到这些限制时,后面需要访问磁盘的进程就 ...
- 动画_ _ Android应用开发之所有动画使用详解
转载: http://blog.csdn.net/yanbober/article/details/46481171 题外话:有段时间没有更新博客了,这篇文章也是之前写了一半一直放在草稿箱,今天抽空把 ...
- Python招聘需求与技能体系
目前国内的招聘Python,基本都是偏向web后台开发,偶有高大上的数据挖掘&机器学习. 这是之前(2012年)找工作整理的一些JD,在梳理几年来的笔记,顺带理一理 可以以此建立自己的技能体系 ...
- loadrunner负载测试实例
回想起第一次做性能测试,感慨万千,故写下本文,从:设置虚拟用户,设置场景以及分析运行结果三个方面进行阐述 硬件环境:硬盘 1TG,cpu 3.40GHz,内存4G 软件环境:IE9.0,Weblogi ...