使用Python寫的第一個網路爬蟲程式,python爬蟲

來源:互聯網
上載者:User

使用Python寫的第一個網路爬蟲程式,python爬蟲

今天嘗試使用python寫一個網路爬蟲代碼,主要是想訪問某個網站,從中選取感興趣的資訊,並將資訊按照一定的格式儲存早Excel中。


此代碼中主要使用到了python的以下幾個功能,由於對python不熟悉,把代碼也粘貼在下面。


1, 使用url開啟網站網頁

import urllib2data = urllib2.urlopen(string_full_link).read().decode('utf8')

print data

2,使用Regex匹配

import re#一般的英文匹配reg = """a href=\S* target='_blank' title=\S*"""dicList = re.compile(reg).findall(data)print dicList
#中文的正則匹配,需要使用中文對應的unicode碼reg=u"\u5730\u5740\S*"      #“地址”對應的 unicode codeaddrList = re.compile(reg).findall(sub_data)
print addrList

3,寫資料到excel檔案

import xlrdimport xlwt        file = xlwt.Workbook()        table = file.add_sheet('hk', cell_overwrite_ok=True)        print index, name, addr, tel        table.write(index, 0, name)        table.write(index, 1, addr)        table.write(index, 2, tel)                file.save("""D:\\test.xls""")


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.