这是一篇不应该写的文章,都写了,针对特定“方式”的爬虫也就没法爬了。

1、模拟登录的一些文章:

解析新浪微博的登录过程(2013-12-23): http://www.cnblogs.com/houkai/p/3487816.html
Python模拟登陆新浪微博(2013-12-24): http://www.cnblogs.com/houkai/p/3488468.html
2014_新浪微博模拟登陆_python(2014-08-08): http://blog.csdn.NET/springzfx/article/details/38435069
Wap的weibo.cn模拟登录@2012年11月8日: http://qinxuye.me/article/simulate-weibo-login-in-python/
wap端模拟登陆withPython@2015-04-15: http://www.cnblogs.com/manqing/p/4428418.html

2、Chrome下登录请求的所有信息,其中像random字段必须通过先get一次登录页来得到,通过分析chrome开发工具下的这些信息基本就Ok,再就是可以复制curl指令看看也很方便:

---------------
General Info:
Remote Address:180.149.153.4:80
Request URL:http://login.weibo.cn/login/?rand=1623920428&backURL=http%3A%2F%2Fweibo.cn&backTitle=%E6%89%8B%E6%9C%BA%E6%96%B0%E6%B5%AA%E7%BD%91&vt=4
Request Method:POST
Status Code:302 Found
----------------------
Response Headers
view source
Connection:close
Content-Encoding:gzip
Content-Length:20
Content-Type:text/html
Date:Thu, 05 Nov 2015 08:11:34 GMT
Location:http://newlogin.sina.cn/crossDomain/?g=4ugB9b801ltp<span style="font-family: Arial, Helvetica, sans-serif;">*************</span>Q&t=1446711094&m=05b4&r=&u=http%3A%2F%2Fweibo.cn%3Fgsid%3D4ugB9b801ltp<span style="font-family: Arial, Helvetica, sans-serif;">*************</span>Q%26PHPSESSID%3D%****%3D4&cross=1&st=ST-NTc0MDUwMTQ4Ng==-1446711094-tc-F903CC8EC38C7F499D7D227DA862A39E,ST-NTc0MDUwMTQ4Ng==-1446711094-tc-F4B911C868B51E768875EB15BBAE6E83&vt=4
Server:Apache
Set-Cookie:SUB=_2A257P39mDeTxGeNJ71IU8C_IwzqIHXVYwAEurDV6PUJbrdAKLVjSkW03kbkKkoC7qLmx-EiB5I-Diw91GQ..; expires=Sat, 05-Dec-2015 08:11:34 GMT; path=/; domain=.weibo.cn; httponly
Set-Cookie:gsid_CTandWM=4ugB9b801ltp*************Q; expires=Sat, 05-Dec-2015 08:11:34 GMT; path=/; domain=.weibo.cn; httponly
SINA-LB:aGEuMTY1LmcxLnlmLmxiLnNpbmFub2RlLmNvbQ==
SINA-TS:MzU2ZWMzNjggMCAwIDAgNSA5Mwo=
Vary:Accept-Encoding
x-debug:172.16.140.196
X-Log-Uid:5740501486
------------------
Request Headers view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:208 Cookie:_T_WM=086561716ab68ff3c6e589cc5152ed66
DNT:1
Host:login.weibo.cn
Origin:http://login.weibo.cn
Referer:http://login.weibo.cn/login/
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
-------------------
Query String Parameters
view source
view URL encoded
rand:1623920428
backURL:http://weibo.cn
backTitle:手机新浪网
vt:4 -------------------
Form Data
view source
view URL encoded
mobile:username@sina.cn
password_4509:user_password
remember:on
backURL:http%3A%2F%2Fweibo.cn
backTitle:手机新浪网
tryCount:
vk:4509_290b_1897615582
submit:登录

3、上一个在pyspider上能走通weibo.cn爬取示例代码,这个代码的缺憾就是如果频繁登陆可能会出验证码,目前还没加验证码识别特性。

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2015-11-05 11:41:06
# Project: sinaweibo
# SinaUser:username@sina.cn
from pyspider.libs.base_handler import * class Handler(BaseHandler):
crawl_config = {
'headers': {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko)',
}
} @every(minutes=24 * 60)
def on_start(self): self.crawl('http://login.weibo.cn/login/', callback=self.login) @config(age=1 * 24 * 60 * 60)
def login(self, response):
cookies = response.cookies
base_url = 'http://login.weibo.cn/login/'
url = base_url + response.doc("form").attr("action")
data = {}
for each in response.doc("form input"):
data[each.name]=each.value
if each.name == "mobile":
data["mobile"] = "username@sina.cn"
if each.type == "password":
data[each.name] = "password"
headers = {}
headers["Content-Type"]="application/x-www-form-urlencoded"
headers["Referer"]="http://login.weibo.cn/login/"
headers["Origin"]="http://login.weibo.cn"
headers["Referer"]="http://login.weibo.cn/login/"
self.crawl(url, callback=self.login_ok,data=data,cookies=cookies,headers=headers,method="POST") @config(priority=2)
def login_ok(self, response):
'''''
return {
"url": response.url,
"headers": response.headers,
"cookies": response.cookies,
"status":response.status_code,
"text":response.text,
}
''' self.crawl("http://weibo.cn/yumayin",
cookies=response.cookies,callback=self.index_page) def index_page(self, response): weibos = []
for each in response.doc("div.c").items():
#each.find("span.kt"):
if each.find("span.kt").html()!= None:
continue
#if each.find("span.ctt")!= None:
if each.find("span.ctt").html()!= None:
weibos.append(each.find("span.ctt").html())
return weibos

4、phantomjs启动的一个问题,官方的文档好像有点问题,真正的启动命令是启动phantomjs proxy选项:

pyspider --phantomjs-proxy="localhost:25555" -c config.json 

这个解决方法是参考:http://blog.csdn.net/jxnu_xiaobing/article/details/44983757

还有可以把运行组件改成all,也可以全部启动:

pyspider  -c config.json all  

5、启用phantomjs以后,js抓取返回HTML是乱码的问题,官方给出的回答:Phantomjs doesn't support gzip, don't set Accept-Encoding header with gzip.

6、重复任务的判定问题,get某登录地址,然后post该登录地址登录,第二步操作可能失败,pyspider判定重复任务完全看URL地址,部署以后,第二个请求会因为age设定问题,直接被拒。

7、DB数据库选择的问题,谈这个问题,首先说说存储方式,projectdb和taskdb,一般我们用户不用访问,就不用说了,resultdb里面,除了我们解析返回的result数据,同时存储的还有id、updatetime、url,而我们返回的result数据会被json序列化为二进制字符串,这种字符串只包含字母之类键盘有的字符串,所以你不论用MongoDB还是RDB,存储其实都是一层数据,不过在resultdb里面,如果我们做挂钩存储的话,mongodb会方便一点,起码不用改数据定义模式。做这类对pyspider的hacking开发,可以到pyspider的目录里面直接修改代码。

8、官网docs上没有Redis带密码连接字符串,先后在github issues和django-redis文档上找到这个设置格式,后者的更全面一点:

redis://[:password]@localhost:6379/0
rediss://[:password]@localhost:6379/0
unix://[:password]@/path/to/socket.sock?db=0

不过要使用这个带password的连接字符串,pyspider版本得升级到最新版本。

9、每个爬虫的入口函数一定要用@every来装饰,千万不要写成别的周期控制装饰器,否则你的这个爬虫项目永远不会被调度器启动。

Sina微博爬取@pyspider的更多相关文章

  1. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  2. python3使用requests爬取新浪热门微博

    微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...

  3. 微博API怎么爬取其它未授权用户的微博/怎么爬取指定用户公布的微博

    获取某个用户最新发表的微博列表:http://open.weibo.com/wiki/2/statuses/user_timeline 原接口已经被封.很多人都在问怎么获取指定用户的微博,于是写这篇B ...

  4. 【python网络编程】新浪爬虫:关键词搜索爬取微博数据

    上学期参加了一个大数据比赛,需要抓取大量数据,于是我从新浪微博下手,本来准备使用新浪的API的,无奈新浪并没有开放关键字搜索的API,所以只能用爬虫来获取了.幸运的是,新浪提供了一个高级搜索功能,为我 ...

  5. 用python爬取微博数据并生成词云

    很早之前写过一篇怎么利用微博数据制作词云图片出来,之前的写得不完整,而且只能使用自己的数据,现在重新整理了一下,任何的微博数据都可以制作出来,放在今天应该比较应景. 一年一度的虐汪节,是继续蹲在角落默 ...

  6. python 爬取腾讯微博并生成词云

    本文以延参法师的腾讯微博为例进行爬取并分析 ,话不多说 直接附上源代码.其中有比较详细的注释. 需要用到的包有 BeautifulSoup WordCloud jieba # coding:utf-8 ...

  7. pyspider爬虫框架webui简介-爬取阿里招聘信息

    命令行输入pyspider开启pyspider 浏览器打开http://localhost:5000/ group表示组名,几个项目可以同一个组名,方便管理,当组名修改为delete时,项目会在一天后 ...

  8. 使用pyspider爬取巨量淘宝MM图片

    具体搭建步骤不再赘述,这里主要使用到了fakeagent,phantomjs和proxy pyspider的爬取相当智能,在不能获取图片的时候会适当的暂停一段时间再试探性的爬取,配合fakeagent ...

  9. pyspider示例代码四:搜索引擎爬取

    搜索引擎爬取 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: __git_lab_fix fr ...

随机推荐

  1. Cocos2D:塔防游戏制作之旅(十)

    最终,draw方法显示这些路径点被放置在哪里,并且绘制出路径点之间的连线,它们仅仅被用作调试.一个成品游戏不应该绘制敌人的路径 - 那对于玩家来说太过容易了! 创建路径点的列表.打开HelloWorl ...

  2. Oracle R12 多组织访问的控制 - MOAC(Multi-Org Access Control)

    什么是MOAC MOAC(Multi-Org Access Control)为多组织访问控制,是Oracle EBS R12的重要新功能,它可以实现在一个Responsibility下对多个Opera ...

  3. ZooKeeper实现命名服务

    使用场景  命名服务就是提供名称的服务,Zookeeper的命名服务有两个应用方面.一个是提供类似JNDI功能,另一个是制作分布式的序列号生成器.         JNDI功能,我们利用Zookeep ...

  4. 学习pthreads,使用属性对象创建结合线程和分离线程

    当我们创建了子线程,是让它犹如脱缰之马,信步驰骋,还是如乖巧听话的孩子,时不时教导一下呢?针对这个问题,本文介绍线程的结合和分离,结构分为三个部分,第一部分给出代码示例,第二部分对代码进行讲解,第三部 ...

  5. 【Python】Talk Python To Me Podcast播客

    这是Python相关的一个播客,通过播客的形式给大家讲述python那点事,相关的链接都会列出来,有一些是由文本内容的,如果听不太懂就看看英文原文.不fanqiang的情况下,网页打开没有问题,但是播 ...

  6. Ubuntu 14 安装Skype 4.3

    Ubuntu 14 安装Skype 4.3Step 1: 删除老版本sudo apt-get remove skype skype-bin:i386 skype:i386 sudo apt-get i ...

  7. 神奇的layout_weight属性

    1.概述 在线性布局有时候为了控制一下屏幕的适配,可以使用layout_weight这个属性来设置权重,要注意一点,这个属性只有在Linearlayout中才有效,这个属性往往会随着android:l ...

  8. daemontools安装和使用

    daemontools安装和使用 参考: http://cr.yp.to/daemontools/install.html daemontools用于自动重启进程.当某个关键服务进程崩溃,可以利用da ...

  9. UE4 Hello World 创建第一个UE4工程

    首先先熟悉几个UE4常用的类 AGameMode(控制整个项目的逻辑) The GameMode defines the game being played. It governs thegame r ...

  10. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...