In python, log on to zhihu to get my favorites and save it as a word file,

Source: Internet
Author: User

In python, log on to zhihu to get my favorites and save it as a word file,

This program was completed a long time ago and never been released. I will share it with you if it is not very busy recently.
The BeautifulSoup module and urllib2 module are used for implementation, and then saved as word. The python docx module is used. I will not repeat the installation method after searching a lot on the Internet.

The main function is to log on to zhihu, and then retrieve the questions and answers from my favorites and save them as Word documents so that they can be viewed when there is no network. of course, the answer can also be obtained if there is an image. however, this is still a problem. wait until you have time to modify it.

There is also a regular expression, which should not be too bad... Despise yourself...

Also, all the answers to the question will be saved. let's see if you have time to save the first answer or add the answer to the questions on the page. otherwise, if there are too many favorites, the saved word will surprise you. O (∩) O Haha ~

You may need a verification code when logging in. If you are prompted to enter the verification code, you can see the verification code picture in the folder of the program.

#-*-Coding: UTF-8-*-# log on to zhihu to capture your favorites and save it as wordimport sysreload (sys) sys. setdefaultencoding ('utf-8') import urllibimport urllib2import cookielibimport stringimport refrom bs4 import BeautifulSoupfrom docx import Documentfrom docx import * from docx. shared import Inchesfrom sys import exitimport OS # this is because you need to use socket proxy # import socks # import socket # socks when surfing the Internet. 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': 'alipay', 'email ': '', 'Password':'', 'rememberme ': 'y'} if not OS. path. exists ('myimg '): OS. mkdir ('myimg ') if OS .path.exists('123.docx '): Require') if needed '): OS .remove('checkcode.gif') mydoc = Document () questiontitle = ''# includef 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) failed T: pass strinfo = re. compile (R' <noscript> [\ s \ S] * </noscript> ') imgcontent = strinfo. sub ('', imgcontent) strinfo = re. compile (R'  ') 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"> ',''). r Eplace ('</div>', '') conent = conent. decode ('utf-8') conent = conent. replace ('<br/>', '\ n') conent = dealimg (conent) ### this piece is too complicated. You have time to find out if there are any html-processing modules. 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 = 'bodyt Ext3 ') "file1_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' + m Ylink enterquestionpage (quectionlink) print quectionlink def loginzhihu (): postdatastr = urllib. urlencode (postdata) ''' cj = cookielib. compile () cookie_support = kernel (cj) opener = urllib2.build _ opener (cookie_support, kernel) urllib2.install _ opener (opener) ''' h = kernel (loginurl) request = urllib2.Request (loginurl, postdatastr, headers) request. get_orig In_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 # It was used for testing at the time, obtain a favorite value # count + = 1 # if count = 1: # return def getcheckcode (thehtml): soup = BeautifulSoup (thehtml) div = soup. find ('div ', {'class': 'js-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 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. compile () cookie_support = compile (cj) opener = urllib2.build _ opener (cookie_support, plugin) urllib2.install _ opener (opener) h = compile (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 ()

Well, this is probably the case. If you have any good suggestions or something, leave a message and I will reply as soon as possible. or you can contact me directly on the webpage of the website.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.