Python爬蟲學習筆記1

來源:互聯網
上載者:User

標籤:

加入實驗室,要寫爬蟲,以前寫過java的,新學python練練手

首先是發包程式,架構如下:

首先是POST方式,代碼如下:

 1 import urllib 2 import urllib2 3 url=‘http://someserver.com/cgi-bin/register.cgi‘ 4 user_agent=‘Mozilla/4.0‘ 5 values={ 6     ‘name‘:‘Liuyuqing‘, 7     ‘location‘:‘DUT‘, 8     ‘language‘:‘python‘ 9 }10 headers={11     ‘cookie‘:‘name=LIUYUQING‘,12     ‘User-Agent‘:user_agent13 }14 data=urllib.urlencode(values)15 req=urllib2.Request(url,data,headers)16 response=urllib2.urlopen(req)17 the_page=response.read()

如果Request時沒有data參數,則發送的是GET方式

response.geturl()  #獲得真正的url

response.info()     #獲得返回報文頭

還有一些自訂opener和handler的知識暫時用不到,先不學啦~下面開始正則表達~

===================華麗的分割線===========================

正則表達基本知識不表,直接說python上的應用:

import rereg=re.compile(r‘<title>(.*)</title>‘)list=re.findall(reg,text)for i in list:    print i    pass

最基本的Helloworld就這麼多,剩下的明天再說,睡覺了Zzz...

 

Python爬蟲學習筆記1

聯繫我們

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