擷取網頁原始碼

來源:互聯網
上載者:User

標籤:etc   資料   headers   操作方法   ons   windows   內容   webkit   urllib2   

# -*- coding: utf-8 -*-import urllib2#urllib2 預設的User-Agent是 Python-urllib/2.7#User-Agent是爬蟲和發爬蟲鬥爭的第一步ua_headers = {    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2716.5 Safari/537.36"}#通過urllib2.Request()構造一個請求對象request = urllib2.Request("http://www.baidu.com/",headers = ua_headers)#向指定的url地址發送請求,並返回伺服器響應的類檔案對象response = urllib2.urlopen(request)#伺服器返回的類檔案對象支援Python檔案對象的操作方法#read()就是讀取檔案裡的全部內容,返回字串html = response.read()#response:是伺服器響應的類檔案,除了支援檔案操作的方法外,還支援以下常用方法#返回http的響應碼,成功返回200,4伺服器頁面出錯,5伺服器問題print response.getcode()#返回返回實際資料的實際url,防止有重新導向問題 print response.getcode()#返回伺服器響應的http前序print response.info()print html

 

擷取網頁原始碼

相關文章

聯繫我們

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