您好,登錄后才能下訂單哦!
項(xiàng)目源碼:
# -*- coding:utf-8 -*- from spider import SpiderHTML from multiprocessing import Pool import sys,urllib,http,os,random,re,time __author__ = 'waiting' ''' 使用了第三方的類庫 BeautifulSoup4,請自行安裝 需要目錄下的spider.py文件 運(yùn)行環(huán)境:python3.4,windows7 ''' #收藏夾的地址 url = 'https://www.zhihu.com/collection/30822111' #page參數(shù)改為代碼添加 #本地存放的路徑,不存在會自動創(chuàng)建 store_path = 'E:\\zhihu\收藏夾\\會員才知道的世界' class zhihuCollectionSpider(SpiderHTML): def __init__(self,pageStart, pageEnd, url): self._url = url self._pageStart = int(pageStart) self._pageEnd = int(pageEnd)+1 self.downLimit = 0 #低于此贊同的答案不收錄 def start(self): for page in range(self._pageStart,self._pageEnd): #收藏夾的頁數(shù) url = self._url + '?page='+str(page) content = self.getUrl(url) questionList = content.find_all('div',class_='zm-item') for question in questionList: #收藏夾的每個問題 Qtitle = question.find('h3',class_='zm-item-title') if Qtitle is None: #被和諧了 continue questionStr = Qtitle.a.string Qurl = 'https://www.zhihu.com'+Qtitle.a['href'] #問題題目 Qtitle = re.sub(r'[\\/:*?"<>]','#',Qtitle.a.string) #windows文件/目錄名不支持的特殊符號 try: print('-----正在獲取問題:'+Qtitle+'-----') #獲取到問題的鏈接和標(biāo)題,進(jìn)入抓取 except UnicodeEncodeError: print(r'---問題含有特殊字符無法顯示---') try: Qcontent = self.getUrl(Qurl) except: print('!!!!獲取出錯!!!!!') pass answerList = Qcontent.find_all('div',class_='zm-item-answer zm-item-expanded') self._processAnswer(answerList,Qtitle) #處理問題的答案 time.sleep(5) def _processAnswer(self,answerList,Qtitle): j = 0 for answer in answerList: j = j + 1 upvoted = int(answer.find('span',class_='count').string.replace('K','000')) #獲得此答案贊同數(shù) if upvoted < self.downLimit: continue authorInfo = answer.find('div',class_='zm-item-answer-author-info') #獲取作者信息 author = {'introduction':'','link':''} try: author['name'] = authorInfo.find('a',class_='author-link').string #獲得作者的名字 author['introduction'] = str(authorInfo.find('span',class_='bio')['title']) #獲得作者的簡介 author['link'] = authorInfo.find('a',class_='author-link')['href'] except AttributeError: author['name'] = '匿名用戶'+str(j) except TypeError: #簡介為空的情況 pass #匿名用戶沒有鏈接 file_name = os.path.join(store_path,Qtitle,'info',author['name']+'_info.txt') if os.path.exists(file_name): #已經(jīng)抓取過 continue self.saveText(file_name,'{introduction}\r\n{link}'.format(**author)) #保存作者的信息 print('正在獲取用戶`{name}`的答案'.format(**author)) answerContent = answer.find('div',class_='zm-editable-content clearfix') if answerContent is None: #被舉報(bào)的用戶沒有答案內(nèi)容 continue imgs = answerContent.find_all('img') if len(imgs) == 0: #答案沒有上圖 pass else: self._getImgFromAnswer(imgs,Qtitle,**author) #收錄圖片 def _getImgFromAnswer(self,imgs,Qtitle,**author): i = 0 for img in imgs: if 'inline-image' in img['class']: #不抓取知乎的小圖 continue i = i + 1 imgUrl = img['src'] extension = os.path.splitext(imgUrl)[1] path_name = os.path.join(store_path,Qtitle,author['name']+'_'+str(i)+extension) try: self.saveImg(imgUrl,path_name) #捕獲各種圖片異常,流程不中斷 except: pass #收錄文字 def _getTextFromAnswer(self): pass #命令行下運(yùn)行,例:zhihu.py 1 5 獲取1到5頁的數(shù)據(jù) if __name__ == '__main__': page, limit, paramsNum= 1, 0, len(sys.argv) if paramsNum>=3: page, pageEnd = sys.argv[1], sys.argv[2] elif paramsNum == 2: page = sys.argv[1] pageEnd = page else: page,pageEnd = 1,1 spider = zhihuCollectionSpider(page,pageEnd,url) spider.start()
很多初學(xué)者,對Python的概念都是模糊不清的,Python能做什么,學(xué)的時(shí)候,該按照什么線路去學(xué)習(xí),學(xué)完往哪方面發(fā)展,想深入了解,詳情可以點(diǎn)擊有道云筆記鏈接了解:http://note.youdao.com/noteshare?id=e4fa02e7b56d7909a27674cdb3da08aa
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。