from
sgmllib
import
SGMLParser
02 |
import sys,urllib2,urllib,cookielib
|
03 |
class spider(SGMLParser):
|
04 |
def __init__( self ,email,password):
|
05 |
SGMLParser.__init__( self )
|
06 |
self .h3 = False
|
07 |
self .h3_is_ready = False
|
08 |
self .div = False
|
09 |
self .h3_and_div = False
|
10 |
self .a = False
|
11 |
self .depth = 0
|
12 |
self .names = ""
|
13 |
self .dic = {}
|
14 |
|
15 |
self .email = email
|
16 |
self .password = password
|
17 |
self .domain = 'renren.com'
|
18 |
try :
|
19 |
cookie = cookielib.CookieJar()
|
20 |
cookieProc = urllib2.HTTPCookieProcessor(cookie)
|
21 |
except :
|
22 |
raise
|
23 |
else :
|
24 |
opener = urllib2.build_opener(cookieProc)
|
25 |
urllib2.install_opener(opener)
|
26 |
27 |
def login( self ):
|
28 |
url = 'http://www.renren.com/PLogin.do'
|
29 |
postdata = {
|
30 |
'email' : self .email,
|
31 |
'password' : self .password,
|
32 |
'domain' : self .domain
|
33 |
}
|
34 |
req = urllib2.Request(
|
35 |
url,
|
36 |
urllib.urlencode(postdata)
|
37 |
)
|
38 |
|
39 |
self . file = urllib2.urlopen(req).read()
|
40 |
#print self.file
|
41 |
def start_h3( self ,attrs):
|
42 |
self .h3 = True
|
43 |
def end_h3( self ):
|
44 |
self .h3 = False
|
45 |
self .h3_is_ready = True
|
46 |
|
47 |
def start_a( self ,attrs):
|
48 |
if self .h3 or self .div:
|
49 |
self .a = True
|
50 |
def end_a( self ):
|
51 |
self .a = False
|
52 |
|
53 |
def start_div( self ,attrs):
|
54 |
if self .h3_is_ready = = False :
|
55 |
return
|
56 |
if self .div = = True :
|
57 |
self .depth + = 1
|
58 |
|
59 |
for k,v in attrs:
|
60 |
if k = = 'class' and v = = 'content' :
|
61 |
self .div = True ;
|
62 |
self .h3_and_div = True #h3 and div is connected
|
63 |
def end_div( self ):
|
64 |
if self .depth = = 0 :
|
65 |
self .div = False
|
66 |
self .h3_and_div = False
|
67 |
self .h3_is_ready = False
|
68 |
self .names = ""
|
69 |
if self .div = = True :
|
70 |
self .depth - = 1
|
71 |
def handle_data( self ,text):
|
72 |
#record the name
|
73 |
if self .h3 and self .a:
|
74 |
self .names + = text
|
75 |
#record says
|
76 |
if self .h3 and ( self .a = = False ):
|
77 |
if not text: pass
|
78 |
else : self .dic.setdefault( self .names,[]).append(text)
|
79 |
return
|
80 |
if self .h3_and_div:
|
81 |
self .dic.setdefault( self .names,[]).append(text)
|
82 |
|
83 |
def show( self ):
|
84 |
type = sys.getfilesystemencoding()
|
85 |
for key in self .dic:
|
86 |
print ( (' '.join(key)).replace(' ',' ')).decode(' utf - 8 ').encode( type ), \
|
87 |
( (' '.join(self.dic[key])).replace(' ',' ')).decode(' utf - 8 ').encode( type )
|
88 |
89 |
90 |
91 |
92 |
renrenspider = spider( 'your email' , 'your password' )
|
93 |
renrenspider.login() |
94 |
renrenspider.feed(renrenspider. file )
|
95 |
renrenspider.show() |
相关推荐
本文实例讲述了Python实现登录人人网并抓取新鲜事的方法。分享给大家供大家参考。具体如下: 这里演示了Python登录人人网并抓取新鲜事的方法(抓取后的排版不太美观~~) from sgmllib import SGMLParser import sys...
Python模拟登录网站并抓取网页的方法.pdf Python模拟登录网站并抓取网页的方法.pdf Python模拟登录网站并抓取网页的方法.pdf Python模拟登录网站并抓取网页的方法.pdf Python模拟登录网站并抓取网页的方法.pdf
人人网信息抓取与数据挖掘 人人网安全措施加强了,无法抓取原本没有权限访问的内容了。 不过,通过浏览器可以访问的内容,依旧可以抓取。 环境要求 ubuntu/win7/xp 皆可。 python3.2 --> python2.7 igraph/...
使用python的urllib库实现自动在人人网发状态
Python车牌识别、车牌抓取源程序,运行程序前需先导入cv2和numpy包。
主要介绍了Python实现抓取HTML网页并以PDF文件形式保存的方法,结合实例形式分析了PyPDF2模块的安装及Python抓取HTML页面并基于PyPDF2模块生成pdf文件的相关操作技巧,需要的朋友可以参考下
需要安装wxWidget模块 进程id读取用到了pywin32模块 抓包用到了WinPcap
在Jupyter环境利用Python核实现网页信息的抓取。
利用python抓取网络图片的步骤: 1.根据给定的网址获取网页源代码 2.利用正则表达式把源代码中的图片地址过滤出来 3.根据过滤出来的图片地址下载网络图片
地图瓦片抓取之python
python+scrapy框架编写的淘宝数据抓取爬虫。输入要抓取商品的关键字,抓取相关数据
Python 抓取 图片Python 抓取 图片Python 抓取 图片Python 抓取 图片Python 抓取 图片Python 抓取 图片
Python网络数据抓取代码主要涉及Python爬虫技术,可以用于自动化采集网络上的数据,如网页内容、图片、视频、音频等。这些数据可以用于数据分析、机器学习、自然语言处理等多个领域。 适用人群: Python网络数据...
本文实例讲述了Python实现抓取网页生成Excel文件的方法。分享给大家供大家参考,具体如下: Python抓网页,主要用到了PyQuery,这个跟jQuery用法一样,超级给力 示例代码如下: #-*- encoding:utf-8 -*- import sys...
Python27 豆瓣影评抓取 源码 及插件下载 Python27 豆瓣影评抓取 源码 及插件下载
python抓取