python实现登陆知乎获得个人收藏并保存为word文件


Posted in Python onMarch 16, 2015

这个程序其实很早之前就完成了,一直没有发出了,趁着最近不是很忙就分享给大家.
使用BeautifulSoup模块和urllib2模块实现,然后保存成word是使用python docx模块的,安装方式网上一搜一大堆,我就不再赘述了.

主要实现的功能是登陆知乎,然后将个人收藏的问题和答案获取到之后保存为word文档,以便没有网络的时候可以查阅.当然,答案中如果有图片的话也是可以获取到的.不过这块还是有点问题的.等以后有时间了在修改修改吧.

还有就是正则,用的简直不要太烂…鄙视下自己…

还有,现在是问题的话所有的答案都会保存下来的.看看有时间修改成只保存第一个答案或者收藏页问题的答案吧.要不然如果收藏的太多了的话保存下来的word会吓你一跳的哦.O(∩_∩)O哈哈~

在登陆的时候可能会需要验证码,如果提示输入验证码的话在程序的文件夹下面就可以看到验证码的图片,照着输入就ok了.

# -*- coding: utf-8 -*-
#登陆知乎抓取个人收藏 然后保存为word
import sys
reload(sys) 
sys.setdefaultencoding('utf-8')
import urllib
import urllib2
import cookielib
import string
import re
from bs4 import BeautifulSoup
from docx import Document
from docx import *
from docx.shared import Inches
from sys import exit
import os
 
#这儿是因为在公司上网的话需要使用socket代理
#import socks
#import socket
#socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,"127.0.0.1",8088)
#socket.socket =socks.socksocket
 
loginurl='http://www.zhihu.com/login'
 
headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',} 
 
postdata={
 '_xsrf': 'acab9d276ea217226d9cc94a84a231f7',
 'email': '',
 'password': '',
 'rememberme':'y'  
}
 
if not os.path.exists('myimg'):
  os.mkdir('myimg')
if os.path.exists('123.docx'):
  os.remove('123.docx')
if os.path.exists('checkcode.gif'):
  os.remove('checkcode.gif')
 
mydoc=Document()
questiontitle=''
#----------------------------------------------------------------------
def dealimg(imgcontent):
  soup=BeautifulSoup(imgcontent)
  try:
    for imglink in soup.findAll('img'):
      if imglink is not None :
        myimg= imglink.get('src')
        #print myimg
        if myimg.find('http')>=0:
          imgsrc=urllib2.urlopen(myimg).read()
          imgnamere=re.compile(r'http\S*/')
          imgname=imgnamere.sub('',myimg)
          #print imgname
          with open(u'myimg'+'/'+imgname,'wb') as code:
            code.write(imgsrc)
            mydoc.add_picture(u'myimg/'+imgname,width=Inches(1.25))
  except:
    pass
  strinfo=re.compile(r'<noscript>[\s\S]*</noscript>')
  imgcontent=strinfo.sub('',imgcontent)
  strinfo=re.compile(r'<img class[\s\S]*</>')
  imgcontent=strinfo.sub('',imgcontent)
  #show all
  strinfo=re.compile(r'<a class="toggle-expand[\s\S]*</a>')
  imgcontent=strinfo.sub('',imgcontent)
 
  strinfo=re.compile(r'<a class=" wrap external"[\s\S]*rel="nofollow noreferrer" target="_blank">')
  imgcontent=strinfo.sub('',imgcontent)
  imgcontent=imgcontent.replace('<i class="icon-external"></i></a>','')
 
 
  imgcontent=imgcontent.replace('</b>','').replace('</p>','').replace('<p>','').replace('<p>','').replace('<br>','')
  return imgcontent
   
 
 
 
 
def enterquestionpage(pageurl):
  html=urllib2.urlopen(pageurl).read()
  soup=BeautifulSoup(html)
  questiontitle=soup.title.string
  mydoc.add_heading(questiontitle,level=3)
  for div in soup.findAll('div',{'class':'fixed-summary zm-editable-content clearfix'}):
    #print div
    conent=str(div).replace('<div class="fixed-summary zm-editable-content clearfix">','').replace('</div>','')
     
    conent=conent.decode('utf-8')
    conent=conent.replace('<br/>','\n')
     
    conent=dealimg(conent)
    ###这一块弄得太复杂了 有时间找找看有没有处理html的模块
    conent=conent.replace('<div class="fixed-summary-mask">','').replace('<blockquote>','').replace('<b>','').replace('<strong>','').replace('</strong>','').replace('<em>','').replace('</em>','').replace('</blockquote>','')
    mydoc.add_paragraph(conent,style='BodyText3')
    """file=open('222.txt','a')
    file.write(str(conent))
    file.close()"""
     
 
def entercollectpage(pageurl):
  html=urllib2.urlopen(pageurl).read()
  soup=BeautifulSoup(html)
  for div in soup.findAll('div',{'class':'zm-item'}):
    h2content=div.find('h2',{'class':'zm-item-title'})
    #print h2content
    if h2content is not None:
      link=h2content.find('a')
      mylink=link.get('href')
      quectionlink='http://www.zhihu.com'+mylink
      enterquestionpage(quectionlink)
      print quectionlink    
 
 
 
def loginzhihu():
  postdatastr=urllib.urlencode(postdata)
  '''
  cj = cookielib.LWPCookieJar()
  cookie_support = urllib2.HTTPCookieProcessor(cj)
  opener = urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
  urllib2.install_opener(opener)
  '''
  h = urllib2.urlopen(loginurl)
  request = urllib2.Request(loginurl,postdatastr,headers)
  request.get_origin_req_host
  response = urllib2.urlopen(request)
  #print response.geturl()
  text = response.read()
 
 
  collecturl='http://www.zhihu.com/collections'
  req=urllib2.urlopen(collecturl)
  if str(req.geturl())=='http://www.zhihu.com/?next=%2Fcollections':
    print 'login fail!'
    return
  txt=req.read()
 
  soup=BeautifulSoup(txt)
  count=0
  divs =soup.findAll('div',{'class':'zm-item'})
  if divs is None:
    print 'login fail!'
    return
  print 'login ok!\n'
  for div in divs:
     
    link=div.find('a')
    mylink=link.get('href')
    collectlink='http://www.zhihu.com'+mylink
    entercollectpage(collectlink)
    print collectlink
    #这儿是当时做测试用的,值获取一个收藏
    #count+=1
    #if count==1:
    #  return
     
 
def getcheckcode(thehtml):
  soup=BeautifulSoup(thehtml)
  div=soup.find('div',{'class':'js-captcha captcha-wrap'})
  if div is not None:
    #print div
    imgsrc=div.find('img')
    imglink=imgsrc.get('src')
    if imglink is not None:
      imglink='http://www.zhihu.com'+imglink
 
      imgcontent=urllib2.urlopen(imglink).read()
      with open('checkcode.gif','wb') as code:
        code.write(imgcontent)
      return True
    else:
      return False
  return False
 
 
if __name__=='__main__':
   
  import getpass
  username=raw_input('input username:')
  password=getpass.getpass('Enter password: ') 
   
  postdata['email']=username
  postdata['password']=password
  postdatastr=urllib.urlencode(postdata)
  cj = cookielib.LWPCookieJar()
  cookie_support = urllib2.HTTPCookieProcessor(cj)
  opener = urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
  urllib2.install_opener(opener)
 
  h = urllib2.urlopen(loginurl)
  request = urllib2.Request(loginurl,postdatastr,headers)
  response = urllib2.urlopen(request)
  txt = response.read()
 
  if getcheckcode(txt):
    checkcode=raw_input('input checkcode:')
    postdata['captcha']=checkcode
    loginzhihu()
    mydoc.save('123.docx')
  else:
    loginzhihu()
    mydoc.save('123.docx')
 
  print 'the end'
  raw_input()

好了,大概就是这样,大家如果有什么好的建议或者什么的可以再下面留言,我会尽快回复的.或者在小站的关于页面有我的联系方式,直接联系我就ok.

Python 相关文章推荐
Python复制目录结构脚本代码分享
Mar 06 Python
使用Python构建Hopfield网络的教程
Apr 14 Python
Python实现批量下载文件
May 17 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
Mar 30 Python
Python使用matplotlib 模块scatter方法画散点图示例
Sep 27 Python
查看keras各种网络结构各层的名字方式
Jun 11 Python
python hmac模块验证客户端的合法性
Nov 07 Python
详解Python调用系统命令的六种方法
Jan 28 Python
Python .py生成.pyd文件并打包.exe 的注意事项说明
Mar 04 Python
Python re.sub 反向引用的实现
Jul 07 Python
Python内置的数据类型及使用方法
Apr 13 Python
Python查找算法的实现 (线性、二分,分块、插值查找算法)
Apr 24 Python
Python标准库urllib2的一些使用细节总结
Mar 16 #Python
python实现查询苹果手机维修进度
Mar 16 #Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 #Python
python实现简单的计时器功能函数
Mar 14 #Python
python将图片文件转换成base64编码的方法
Mar 14 #Python
python在Windows8下获取本机ip地址的方法
Mar 14 #Python
python检测远程端口是否打开的方法
Mar 14 #Python
You might like
php汉字转拼音的示例
2014/02/27 PHP
非常好用的Zend Framework分页类
2014/06/25 PHP
php发送html格式文本邮件的方法
2015/06/10 PHP
在WordPress中使用PHP脚本来判断访客来自什么国家
2015/12/10 PHP
javascript下过滤数组重复值的代码
2007/09/10 Javascript
javascript cookies操作集合
2010/04/12 Javascript
jQuery动态设置form表单的enctype值(实现代码)
2013/07/04 Javascript
javascript中的parseInt和parseFloat区别
2013/07/12 Javascript
JS排序方法(sort,bubble,select,insert)代码汇总
2016/01/30 Javascript
JavaScript中的时间处理小结
2016/02/24 Javascript
JS实现pasteHTML兼容ie,firefox,chrome的方法
2016/06/22 Javascript
详解nodejs微信公众号开发——4.自动回复各种消息
2017/04/11 NodeJs
JavaScript中发出HTTP请求最常用的方法
2018/07/12 Javascript
jQuery实现的鼠标拖动画矩形框示例【可兼容IE8】
2019/05/17 jQuery
layui--js控制switch的切换方法
2019/09/03 Javascript
python使用in操作符时元组和数组的区别分析
2015/05/19 Python
python自动翻译实现方法
2016/05/28 Python
利用nohup来开启python文件的方法
2019/01/14 Python
使用Python做定时任务及时了解互联网动态
2019/05/15 Python
python自动发邮件总结及实例说明【推荐】
2019/05/31 Python
python将时分秒转换成秒的实例
2019/12/07 Python
python简单实现9宫格图片实例
2020/09/03 Python
推荐WEB开发者最佳HTML5和CSS3代码生成器
2015/11/24 HTML / CSS
北美个性化礼品商店:Things Remembered
2018/06/12 全球购物
Edwaybuy西班牙:小米在线商店
2019/12/04 全球购物
黑猩猩商店:The Chimp Store
2020/02/12 全球购物
公司员工的自我评价范例
2013/11/01 职场文书
医校毕业生自我鉴定
2014/01/25 职场文书
业务部门经理岗位职责
2014/02/23 职场文书
公司请假条范文
2014/04/11 职场文书
2014年小学美术工作总结
2014/12/20 职场文书
地陪导游欢迎词
2015/01/26 职场文书
基层医务人员三严三实心得体会
2016/01/05 职场文书
《蟋蟀的住宅》教学反思
2016/02/17 职场文书
解决redis sentinel 频繁主备切换的问题
2021/04/12 Redis
详解Java实现数据结构之并查集
2021/06/23 Java/Android