<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 最新動態 > 更新時間:2015/8/2 > > 最近好多讀者反映代碼已經不能用了,原因是淘寶索引頁的MM鏈接改了。網站改版了,URL的索引已經和之前的不一樣了,之前可以直接跳轉到每個MM的個性域名,現在中間加了一個跳轉頁,本以為可以通過這個頁面然后跳轉到原來的個性域名,而經過一番折騰發現,這個跳轉頁中的內容是JS動態生成的,所以不能用Urllib庫來直接抓取了,本篇就只提供學習思路,代碼不能繼續用了。 > > 之后博主會利用其它方法來嘗試解決,如果解決,第一時間更新!謝謝大家! > 更新時間:2016/3/26 > > 如上問題已解決,利用 PhantomJS的動態解析即可完成。因為 PySpider 同樣支持 PhantomJS,所以我直接利用了 PySpider 來完成,解決方案如下 > > [解決方案](http://cuiqingcai.com/2652.html) > > 另外如果不想使用框架,可以直接利用 Selenium + PhantomJS 來解析,同樣方便,解決方案可以參考 > > [動態解析解決方案](http://cuiqingcai.com/2599.html) ## 本篇目標 1.抓取淘寶MM的姓名,頭像,年齡 2.抓取每一個MM的資料簡介以及寫真圖片 3.把每一個MM的寫真圖片按照文件夾保存到本地 4.熟悉文件保存的過程 ## 1.URL的格式 在這里我們用到的URL是?[http://mm.taobao.com/json/request_top_list.htm?page=1](http://mm.taobao.com/json/request_top_list.htm?page=1),問號前面是基地址,后面的參數page是代表第幾頁,可以隨意更換地址。點擊開之后,會發現有一些淘寶MM的簡介,并附有超鏈接鏈接到個人詳情頁面。 我們需要抓取本頁面的頭像地址,MM姓名,MM年齡,MM居住地,以及MM的個人詳情頁面地址。 ## 2.抓取簡要信息 相信大家經過上幾次的實戰,對抓取和提取頁面的地址已經非常熟悉了,這里沒有什么難度了,我們首先抓取本頁面的MM詳情頁面地址,姓名,年齡等等的信息打印出來,直接貼代碼如下 ~~~ __author__ = 'CQC' # -*- coding:utf-8 -*- import urllib import urllib2 import re class Spider: ????def __init__(self): ????????self.siteURL = 'http://mm.taobao.com/json/request_top_list.htm' ????def getPage(self,pageIndex): ????????url = self.siteURL + "?page=" + str(pageIndex) ????????print url ????????request = urllib2.Request(url) ????????response = urllib2.urlopen(request) ????????return response.read().decode('gbk') ????def getContents(self,pageIndex): ????????page = self.getPage(pageIndex) ????????pattern = re.compile('(.*?).*?(.*?).*?(.*?)',re.S) ????????items = re.findall(pattern,page) ????????for item in items: ????????????print item[0],item[1],item[2],item[3],item[4] spider = Spider() spider.getContents(1) ~~~ 運行結果如下 [![](https://box.kancloud.cn/2016-05-29_574a95df430bf.jpg)](http://qiniu.cuiqingcai.com/wp-content/uploads/2015/02/QQ%E6%88%AA%E5%9B%BE20150220234132.jpg) ## ?2.文件寫入簡介 在這里,我們有寫入圖片和寫入文本兩種方式 ### 1)寫入圖片 ~~~ #傳入圖片地址,文件名,保存單張圖片 def saveImg(self,imageURL,fileName): ???? u = urllib.urlopen(imageURL) ???? data = u.read() ???? f = open(fileName, 'wb') ???? f.write(data) ???? f.close() ~~~ ### 2)寫入文本 ~~~ def saveBrief(self,content,name): ????fileName = name + "/" + name + ".txt" ????f = open(fileName,"w+") ????print u"正在偷偷保存她的個人信息為",fileName ????f.write(content.encode('utf-8')) ~~~ ### 3)創建新目錄 ~~~ #創建新目錄 def mkdir(self,path): ????path = path.strip() ????# 判斷路徑是否存在 ????# 存在???? True ????# 不存在?? False ????isExists=os.path.exists(path) ????# 判斷結果 ????if not isExists: ????????# 如果不存在則創建目錄 ????????# 創建目錄操作函數 ????????os.makedirs(path) ????????return True ????else: ????????# 如果目錄存在則不創建,并提示目錄已存在 ????????return False ~~~ ## 3.代碼完善 主要的知識點已經在前面都涉及到了,如果大家前面的章節都已經看了,完成這個爬蟲不在話下,具體的詳情在此不再贅述,直接帖代碼啦。 ~~~ spider.py ~~~ ~~~ __author__ = 'CQC' # -*- coding:utf-8 -*- import urllib import urllib2 import re import tool import os #抓取MM class Spider: ????#頁面初始化 ????def __init__(self): ????????self.siteURL = 'http://mm.taobao.com/json/request_top_list.htm' ????????self.tool = tool.Tool() ????#獲取索引頁面的內容 ????def getPage(self,pageIndex): ????????url = self.siteURL + "?page=" + str(pageIndex) ????????request = urllib2.Request(url) ????????response = urllib2.urlopen(request) ????????return response.read().decode('gbk') ????#獲取索引界面所有MM的信息,list格式 ????def getContents(self,pageIndex): ????????page = self.getPage(pageIndex) ????????pattern = re.compile('(.*?).*?(.*?).*?(.*?)',re.S) ????????items = re.findall(pattern,page) ????????contents = [] ????????for item in items: ????????????contents.append([item[0],item[1],item[2],item[3],item[4]]) ????????return contents ????#獲取MM個人詳情頁面 ????def getDetailPage(self,infoURL): ????????response = urllib2.urlopen(infoURL) ????????return response.read().decode('gbk') ????#獲取個人文字簡介 ????def getBrief(self,page): ????????pattern = re.compile('(.*?),re.S) ????????result = re.search(pattern,page) ????????return self.tool.replace(result.group(1)) ????#獲取頁面所有圖片 ????def getAllImg(self,page): ????????pattern = re.compile('(.*?),re.S) ????????#個人信息頁面所有代碼 ????????content = re.search(pattern,page) ????????#從代碼中提取圖片 ????????patternImg = re.compile(',re.S) ????????images = re.findall(patternImg,content.group(1)) ????????return images ????#保存多張寫真圖片 ????def saveImgs(self,images,name): ????????number = 1 ????????print u"發現",name,u"共有",len(images),u"張照片" ????????for imageURL in images: ????????????splitPath = imageURL.split('.') ????????????fTail = splitPath.pop() ????????????if len(fTail) > 3: ????????????????fTail = "jpg" ????????????fileName = name + "/" + str(number) + "." + fTail ????????????self.saveImg(imageURL,fileName) ????????????number += 1 ????# 保存頭像 ????def saveIcon(self,iconURL,name): ????????splitPath = iconURL.split('.') ????????fTail = splitPath.pop() ????????fileName = name + "/icon." + fTail ????????self.saveImg(iconURL,fileName) ????#保存個人簡介 ????def saveBrief(self,content,name): ????????fileName = name + "/" + name + ".txt" ????????f = open(fileName,"w+") ????????print u"正在偷偷保存她的個人信息為",fileName ????????f.write(content.encode('utf-8')) ????#傳入圖片地址,文件名,保存單張圖片 ????def saveImg(self,imageURL,fileName): ???????? u = urllib.urlopen(imageURL) ???????? data = u.read() ???????? f = open(fileName, 'wb') ???????? f.write(data) ???????? print u"正在悄悄保存她的一張圖片為",fileName ???????? f.close() ????#創建新目錄 ????def mkdir(self,path): ????????path = path.strip() ????????# 判斷路徑是否存在 ????????# 存在???? True ????????# 不存在?? False ????????isExists=os.path.exists(path) ????????# 判斷結果 ????????if not isExists: ????????????# 如果不存在則創建目錄 ????????????print u"偷偷新建了名字叫做",path,u'的文件夾' ????????????# 創建目錄操作函數 ????????????os.makedirs(path) ????????????return True ????????else: ????????????# 如果目錄存在則不創建,并提示目錄已存在 ????????????print u"名為",path,'的文件夾已經創建成功' ????????????return False ????#將一頁淘寶MM的信息保存起來 ????def savePageInfo(self,pageIndex): ????????#獲取第一頁淘寶MM列表 ????????contents = self.getContents(pageIndex) ????????for item in contents: ????????????#item[0]個人詳情URL,item[1]頭像URL,item[2]姓名,item[3]年齡,item[4]居住地 ????????????print u"發現一位模特,名字叫",item[2],u"芳齡",item[3],u",她在",item[4] ????????????print u"正在偷偷地保存",item[2],"的信息" ????????????print u"又意外地發現她的個人地址是",item[0] ????????????#個人詳情頁面的URL ????????????detailURL = item[0] ????????????#得到個人詳情頁面代碼 ????????????detailPage = self.getDetailPage(detailURL) ????????????#獲取個人簡介 ????????????brief = self.getBrief(detailPage) ????????????#獲取所有圖片列表 ????????????images = self.getAllImg(detailPage) ????????????self.mkdir(item[2]) ????????????#保存個人簡介 ????????????self.saveBrief(brief,item[2]) ????????????#保存頭像 ????????????self.saveIcon(item[1],item[2]) ????????????#保存圖片 ????????????self.saveImgs(images,item[2]) ????#傳入起止頁碼,獲取MM圖片 ????def savePagesInfo(self,start,end): ????????for i in range(start,end+1): ????????????print u"正在偷偷尋找第",i,u"個地方,看看MM們在不在" ????????????self.savePageInfo(i) #傳入起止頁碼即可,在此傳入了2,10,表示抓取第2到10頁的MM spider = Spider() spider.savePagesInfo(2,10) ~~~ ~~~ tool.py ~~~ ~~~ __author__ = 'CQC' #-*- coding:utf-8 -*- import re #處理頁面標簽類 class Tool: ????#去除img標簽,1-7位空格,&nbsp; ????removeImg = re.compile('| {1,7}|&nbsp;') ????#刪除超鏈接標簽 ????removeAddr = re.compile('|') ????#把換行的標簽換為\n ????replaceLine = re.compile('|||') ????#將表格制表替換為\t ????replaceTD= re.compile('') ????#將換行符或雙換行符替換為\n ????replaceBR = re.compile('|') ????#將其余標簽剔除 ????removeExtraTag = re.compile('') ????#將多行空行刪除 ????removeNoneLine = re.compile('\n+') ????def replace(self,x): ????????x = re.sub(self.removeImg,"",x) ????????x = re.sub(self.removeAddr,"",x) ????????x = re.sub(self.replaceLine,"\n",x) ????????x = re.sub(self.replaceTD,"\t",x) ????????x = re.sub(self.replaceBR,"\n",x) ????????x = re.sub(self.removeExtraTag,"",x) ????????x = re.sub(self.removeNoneLine,"\n",x) ????????#strip()將前后多余內容刪除 ????????return x.strip() ~~~ 以上兩個文件就是所有的代碼內容,運行一下試試看,那叫一個酸爽啊 [![](image/574a890114dab.jpg)](http://qiniu.cuiqingcai.com/wp-content/uploads/2015/02/QQ%E6%88%AA%E5%9B%BE20150221020543.jpg) 看看文件夾里面有什么變化 [![](https://box.kancloud.cn/2016-05-29_574a95df7c221.jpg)](http://qiniu.cuiqingcai.com/wp-content/uploads/2015/02/QQ%E6%88%AA%E5%9B%BE20150221020709.jpg) [![](https://box.kancloud.cn/2016-05-29_574a95dfa0afa.jpg)](http://qiniu.cuiqingcai.com/wp-content/uploads/2015/02/QQ%E6%88%AA%E5%9B%BE20150221021032.jpg) 不知不覺,海量的MM圖片已經進入了你的電腦,還不快快去試試看!! 代碼均為本人所敲,寫的不好,大神勿噴,寫來方便自己,同時分享給大家參考!希望大家支持!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看