亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      python

      當(dāng)前位置:中華考試網(wǎng) >> python >> python爬蟲 >> 文章內(nèi)容

      python數(shù)據(jù)爬下來保存在哪里?

      來源:中華考試網(wǎng)  [2020年10月9日]  【

         python數(shù)據(jù)爬下來保存在本地,一般是文件或數(shù)據(jù)庫中,但是文件形式相比要更加簡(jiǎn)單,如果只是自己寫爬蟲玩,可以用文件形式來保存數(shù)據(jù)。

        #coding=utf-8

        import urllib.request

        import re

        import os

        '''

        XPath helper插件是chrome的一個(gè)插件,基于chrome核的瀏覽器也可以安裝。XPath helper可以用來調(diào)試XPath表達(dá)式。

      python課程免費(fèi)試聽預(yù)約

      • 地區(qū):
      • 姓名:
      • 手機(jī):

        Urllib 模塊提供了讀取web頁面數(shù)據(jù)的接口,我們可以像讀取本地文件一樣讀取www和ftp上的數(shù)據(jù)

        urlopen 方法用來打開一個(gè)url

        read方法 用于讀取Url上的數(shù)據(jù)

        '''

        def getHtml(url):

        page = urllib.request.urlopen(url);

        html = page.read();

        return html;

        def getImg(html):

        imglist = re.findall('img src="(http.*?)"',html

        return imglist

        html = getHtml("https://www.zhihu.com/question/34378366").decode("utf-8");

        imagesUrl = getImg(html);

        if os.path.exists("D:/imags") == False:

        os.mkdir("D:/imags");

        count = 0;

        for url in imagesUrl:

        print(url)

        if(url.find('.') != -1):

        name = url[url.find('.',len(url) - 5):];

        bytes = urllib.request.urlopen(url);

        f = open("D:/imags/"+str(count)+name, 'wb');

        f.write(bytes.read());

        f.flush();

        f.close();

        count+=1

        經(jīng)測(cè)試,基本功能還是可以實(shí)現(xiàn)的;ǖ妮^多的時(shí)間就是正則匹配哪里,因?yàn)樽约簩?duì)正則表達(dá)式也不是非常熟悉。所以還是花了點(diǎn)時(shí)間。

      責(zé)編:hym
      • 會(huì)計(jì)考試
      • 建筑工程
      • 職業(yè)資格
      • 醫(yī)藥考試
      • 外語考試
      • 學(xué)歷考試