欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件

 更新時間:2015年03月16日 09:26:56   投稿:junjie  
這篇文章主要介紹了python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下

這個程序其實很早之前就完成了,一直沒有發(fā)出了,趁著最近不是很忙就分享給大家.
使用BeautifulSoup模塊和urllib2模塊實現(xiàn),然后保存成word是使用python docx模塊的,安裝方式網(wǎng)上一搜一大堆,我就不再贅述了.

主要實現(xiàn)的功能是登陸知乎,然后將個人收藏的問題和答案獲取到之后保存為word文檔,以便沒有網(wǎng)絡的時候可以查閱.當然,答案中如果有圖片的話也是可以獲取到的.不過這塊還是有點問題的.等以后有時間了在修改修改吧.

還有就是正則,用的簡直不要太爛…鄙視下自己…

還有,現(xiàn)在是問題的話所有的答案都會保存下來的.看看有時間修改成只保存第一個答案或者收藏頁問題的答案吧.要不然如果收藏的太多了的話保存下來的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
 
#這兒是因為在公司上網(wǎng)的話需要使用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()

好了,大概就是這樣,大家如果有什么好的建議或者什么的可以再下面留言,我會盡快回復的.或者在小站的關于頁面有我的聯(lián)系方式,直接聯(lián)系我就ok.

相關文章

  • Python 獲取異常(Exception)信息的幾種方法

    Python 獲取異常(Exception)信息的幾種方法

    這篇文章主要介紹了Python 獲取異常(Exception)信息的幾種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python圖片存儲和訪問的三種方式詳解

    Python圖片存儲和訪問的三種方式詳解

    在?Python?中處理圖像數(shù)據(jù)的時候,例如應用卷積神經(jīng)網(wǎng)絡等算法可以處理大量圖像數(shù)據(jù)集,這里就需要學習如何用最簡單的方式存儲、讀取數(shù)據(jù)。本文介紹了Python中圖片存儲和訪問的三種方式,需要的可以參考一下
    2022-04-04
  • 一篇文章弄懂Python中的內(nèi)建函數(shù)

    一篇文章弄懂Python中的內(nèi)建函數(shù)

    Python學習,內(nèi)建函數(shù)是你必須要掌握的一部分,下面這篇文章主要給大家介紹了關于Python中內(nèi)建函數(shù)的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • Python學習筆記之列表推導式實例分析

    Python學習筆記之列表推導式實例分析

    這篇文章主要介紹了Python學習筆記之列表推導式,結合實例形式分析Python列表推導式的原理、寫法與相關使用技巧,需要的朋友可以參考下
    2019-08-08
  • Python for 循環(huán)語句的使用

    Python for 循環(huán)語句的使用

    這篇文章主要介紹了Python for 循環(huán)語句,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-06-06
  • python使用裝飾器和線程限制函數(shù)執(zhí)行時間的方法

    python使用裝飾器和線程限制函數(shù)執(zhí)行時間的方法

    這篇文章主要介紹了python使用裝飾器和線程限制函數(shù)執(zhí)行時間的方法,主要涉及timelimited函數(shù)的使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • 使用python把Excel中的數(shù)據(jù)在頁面中可視化

    使用python把Excel中的數(shù)據(jù)在頁面中可視化

    最近學習數(shù)據(jù)分析,感覺Python做數(shù)據(jù)分析真的好用,下面這篇文章主要給大家介紹了關于如何使用python把Excel中的數(shù)據(jù)在頁面中可視化的相關資料,需要的朋友可以參考下
    2022-03-03
  • python數(shù)學模塊(math/decimal模塊)

    python數(shù)學模塊(math/decimal模塊)

    這篇文章主要介紹了python數(shù)學模塊(math/decimal模塊),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • python?kornia計算機視覺庫實現(xiàn)圖像變化

    python?kornia計算機視覺庫實現(xiàn)圖像變化

    這篇文章主要為大家介紹了python?kornia計算機視覺庫實現(xiàn)圖像變化算法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 一文教你徹底解決Python包下載慢問題

    一文教你徹底解決Python包下載慢問題

    在利用python中,我們經(jīng)常需要使用到各種各樣的庫。其中,pip是我們常用的安裝工具,一般情況下我們基本上是直接pip方法安裝第三方包,下面這篇文章主要給大家介紹了關于如何徹底解決Python包下載慢問題的相關資料,需要的朋友可以參考下
    2023-05-05

最新評論