最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 14:31:48
文档

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

python实现登陆知乎获得个人收藏并保存为word文件:这个程序其实很早之前就完成了,一直没有发出了,趁着最近不是很忙就分享给大家. 使用BeautifulSoup模块和urllib2模块实现,然后保存成word是使用python docx模块的,安装方式网上一搜一大堆,我就不再赘述了. 主要实现的功能是登陆知乎,然后将个人收藏的问题和答
推荐度:
导读python实现登陆知乎获得个人收藏并保存为word文件:这个程序其实很早之前就完成了,一直没有发出了,趁着最近不是很忙就分享给大家. 使用BeautifulSoup模块和urllib2模块实现,然后保存成word是使用python docx模块的,安装方式网上一搜一大堆,我就不再赘述了. 主要实现的功能是登陆知乎,然后将个人收藏的问题和答


','') 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! ' 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实现登陆知乎获得个人收藏并保存为word文件

python实现登陆知乎获得个人收藏并保存为word文件:这个程序其实很早之前就完成了,一直没有发出了,趁着最近不是很忙就分享给大家. 使用BeautifulSoup模块和urllib2模块实现,然后保存成word是使用python docx模块的,安装方式网上一搜一大堆,我就不再赘述了. 主要实现的功能是登陆知乎,然后将个人收藏的问题和答
推荐度:
标签: 保存 word 文件
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top