溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python 爬蟲 urllib模塊:get方式

發(fā)布時間:2020-07-07 14:04:20 來源:網(wǎng)絡(luò) 閱讀:378 作者:虎皮喵的喵 欄目:編程語言

本程序以爬取 百度 首頁為例

格式:

  導(dǎo)入urllib.request

  打開爬取的網(wǎng)頁: response = urllib.request.urlopen('網(wǎng)址')

  讀取網(wǎng)頁代碼: html = response.read()

  打印:

      1.不decode 

      print(html) #爬取的網(wǎng)頁代碼會不分行,沒有空格顯示,很難看

      2.decode

      print(html.decode()) #爬取的網(wǎng)頁代碼會分行,像寫規(guī)范的代碼一樣,看起來很舒服

  查詢請求結(jié)果:

      a. response.status # 返回 200:請求成功  404:網(wǎng)頁找不到,請求失敗

      b. response.getcode() # 返回 200:請求成功  404:網(wǎng)頁找不到,請求失敗


1.不decode的程序如下:

import urllib.request

response = urllib.request.urlopen('www.baidu.com')
html = response.read()
print(html)
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)


運(yùn)行結(jié)果:

Python 爬蟲 urllib模塊:get方式



2.decode的程序如下:

import urllib.request

response = urllib.request.urlopen('www.baidu.com')
html = response.read()

print(html.decode())
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)


運(yùn)行結(jié)果:

<!DOCTYPE html>
<!--STATUS OK-->


<html>
<head>
    
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta content="always" name="referrer">
    <meta name="theme-color" content="#2932e1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" />
    <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg">


<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
<link rel="dns-prefetch" href="//t1.baidu.com"/>
<link rel="dns-prefetch" href="//t2.baidu.com"/>
<link rel="dns-prefetch" href="//t3.baidu.com"/>
<link rel="dns-prefetch" href="//t10.baidu.com"/>
<link rel="dns-prefetch" href="//t11.baidu.com"/>
<link rel="dns-prefetch" href="//t12.baidu.com"/>
<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
    
    <title>百度一下,你就知道</title>
    

<style id="css_index" index="index" type="text/css">html,body{height:100%}
.
.
.
.


</body>
</html>






------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
200


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI