python 自動下載網站相關附件

來源:互聯網
上載者:User

標籤:python autoweb download urllib

論壇裡有人問如何寫個簡單的爬蟲. 要求如下:


每天的最新文章裡面的附件

如何做到

定時下載


#看了一下這個網站, 還好. 好久沒折騰過 urllib什麼的, 然後寫了一個, 測試可以.

在正則匹配時, 建議把資料的編碼方式轉為指令碼編碼方式一致.


指令碼如下:

#!/usr/bin/env python#-*- coding:utf-8 -*-import urllibimport reurl = "http://www.bjjy.gov.cn"def get_html_data(u):    h = urllib.urlopen(u).read()    t = ""    for i in h.split("\n"):        try:            t += "\n" + i.decode(‘gbk‘).encode(‘utf-8‘)        except:            pass    return t    def get_link(data):    d, link = re.findall(r‘最新文章標題開始(.*)最新文章標題結束‘, data, re.S), []    if len(d):        s = re.findall(r‘<A HREF="([^"].*)" TARGET="_blank"‘, d[0], re.I)        if len(s):            for i in s:                link.append(i)    return linkif __name__ == "__main__":    html = get_html_data(url)    f_link = get_link(html)    for i in f_link:        tmp_data = get_html_data(url + i)        tmp_link = re.findall(r‘<a href="([^"].*)"><span style‘,tmp_data, re.I)        if len(tmp_link):            for j in tmp_link:                urllib.urlretrieve(url+j, (url+j).split("/")[-1])



上面要匹配 最新文章 那個地方, 之前沒有把資料轉碼,就是不行.


以上指令碼加個定時任務OK.

python 自動下載網站相關附件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.