Request套件

      在〈Request套件〉中尚無留言

安裝

爬蟲,就是用程式把網頁先下載下來,再進行分析。使用 Python 下載網頁上的資料,最基本的作法就是以 requests 模組建立適當的 HTTP 請求,透過 HTTP 請求從網頁伺服器下載指定的資料,絕大部分的網路爬蟲或除錯問題都可以靠這個架構解決,是網路爬蟲必備的工具之一。

使用requests前需先 pip install requests

GET

requests.get()就是把網站上的html全部下載下來再說, 至於分析的事, 交給日後再來處理. 底下是台灣證券交易所的首頁

r.text是整個網頁的html, 因過於長, 所以先註解. 讀者可自行打開測試

import requests
r = requests.get("https://www.twse.com.tw/zh/page/trading/indices/MI_5MINS_HIST.html")
r.encoding="utf-8" #解決中文亂碼的問題
print(r.text)
print(type(r))
print(r.status_code)
print(r.encoding)
print(r.cookies)

#print(r.text) #印出HTML

結果 :
<class 'requests.models.Response'>
200
utf-8
<RequestsCookieJar[<Cookie JSESSIONID=1FC30CEDCF6ABE04548546F754728CA0 for www.twse.com.tw/>]>

下載圖片

直接將 page.content以”wb” 格式寫入檔案內即可

import requests
#page = requests.get("http://www.uuxs.tw/ls/22_22102/")
page = requests.get("/wp-content/uploads/2016/10/img_6279.jpg")
print(page.text)
with open('tiger.jpg','wb') as f:
    f.write(page.content)

POST

網頁的請求傳送,分為GET及POST。GET是將要傳給伺服器的參數,寫在網址後面,比如
http://mahaljsp.asuscomm.com/lcc/login_process.php?userAccount=xxx&userPassword=xxxxxx

但上述將密碼寫在url裏傳送給伺服器,這也太大膽露骨了,會被別人看到密碼的。所以就有了POST的傳遞方式。POST 需使用表單 form 的 submit 指令。

要傳遞出去的資料,需使用字典格式加在 requests 裏的 data 參數。

import requests
param={"userAccount":"xxx", "userPassword":"xxxxxx"}
page=requests.post("http://mahaljsp.asuscomm.com/lcc/login_process.php", data=param)
print(page.text)
結果:
<html>
   <style>
      html, body{
         height : 99%
         margin:0;
      }
   </style>
   <head>
      <title>Mahaljsp</title>
   </head>
   <body>
      <table border="1" cellspacing="0" cellpadding="0" align="center" bgcolor="#00ffff" style="height:100%;width:100%">
         <tr height="50px" bgcolor="#ffff00">
            <td align="center">Mahaljsp</td>
         </tr>
         <tr>
            <td align="center">恭喜你登入成功了</td>
         </tr>
         <tr>
            <td align="right" bgcolor="#00ff00"  height="50px">Power by Thomas</td>
         </tr>        
      </table>
   </body>
</html>

底下是 login_process.php的程式碼

<?php
$userAccount=$_POST["userAccount"];
$userPassword=$_POST["userPassword"];
if ($userAccount=="xxx" && $userPassword=="xxxxxx"){
header("Location:success.php");
}
else{
header("Location:login.php");
}
?>

Header

有些網站,會判斷 header 的內容,比如網頁格式,瀏覽器種類,如果不符合網站的要求,就會拒絕傳送正確的資料給使用者。此時使用 requests 就必需增加 header 參數,可以使用如下方式。

import requests
headers = {'user-agent': 'Mozilla/5.0'}
r = requests.get("http://abc.com.tw",headers=headers)
print(r.url)

Cookies

request取回的網頁資料, 其實是包含了cookies的, 可以使用r.cookies將之印出

print(r.cookies)
print(r.cookies['example_cookie_name'])

requests作不到的事

requests 可以完整取得 html 裏的資料,就連 javascript 也一併取回。所以經由 javascript 演算後的資料,就無法由 requests 取得。

import requests
param={"userAccount":"xxx", "userPassword":"xxxxxx"}
page=requests.get("http://mahaljsp.asuscomm.com/lcc/99.php", param)
print(page.text)
結果 :
<html>
<style>
html, body{
height : 99%
margin:0;
}
</style>
<head>
<title>Mahaljsp</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" align="center" bgcolor="#00ffff">
<script>
for (i=1;i<=9;i++){
document.write("<tr>");
for (j=1;j<=9;j++){
document.write("<td width='50' align='right'>");
document.write(i*j);
document.write("</td>");
}
document.write("</tr>");
}
</script>
</table>
</body>
</html>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *