python的爬蟲入門學習2
0.urlopen()方法的timeout引數用於設定什麼:
用於設定超時時間(答:timeout 引數用於設定連線的超時時間,單位是秒)
1. 如何從 urlopen() 返回的物件中獲取 HTTP 狀態碼?
用getcode()方法
response = urllib.request.urlopen(url)#開啟url網址
code = response.getcode()#獲取HTTP狀態碼
2. 在客戶端和伺服器之間進行請求-響應時,最常用的是哪兩種方法?
答:GET 和 POST。
response=urllib.request.urlopen("url")
或response=urllib.request.Response("url")
Response.urlopen()
3. HTTP 是基於請求-響應的模式,那是客戶端發出請求,服務端做出響應;還是服務端發出請求,客戶端做出響應呢
回答:是客戶端發出請求,伺服器響應
4.User-Agent 屬性通常是記錄什麼資訊
記錄的是使用者與伺服器互動的資訊
答:普通瀏覽器會通過該內容向訪問網站提供你所使用的瀏覽器型別、作業系統、瀏覽器核心等資訊的標識。
5.如何通過 urlopen() 使用 POST 方法向服務端發出請求?
答:urlopen 函式有一個 data 引數,如果給這個引數賦值,那麼 HTTP 的請求就是使用 POST 方式;
如果 data 的值是 NULL,也就是預設值,那麼 HTTP 的請求就是使用 GET 方式
6. 使用字串的什麼方法將其它編碼轉換為 Unicode 編碼
答:decode。decode 的作用是將其他編碼的字串轉換成 unicode 編碼,相反,encode 的作用是將 unicode 編碼轉換成其他編碼的字串
html=response.read().decode(" Unicode ")
7. JSON 是什麼鬼
答:JSON 是一種輕量級的資料交換格式,說白了這裡就是用字串把 Python 的資料結構封裝起來,便與儲存和使用
是一種傳遞物件的語法,物件可以是name/value 陣列
8.配合 EasyGui,給“下載一隻貓“的程式碼增加互動(關於easygui安裝)
- 讓使用者輸入尺寸;
- 如果使用者不輸入尺寸,那麼按預設寬400,高600下載喵;
- 讓使用者指定儲存位置
import easygui as g
import urllib.request
def main():
msg = "請填寫喵的尺寸"
title = "下載一隻喵"
fieldNames = ["寬:", "高:"]
fieldValues = []
size = width, height = 400, 600
fieldValues = g.multenterbox(msg, title, fieldNames, size)
while 1:
if fieldValues == None:
break
errmsg = ""
try:
width = int(fieldValues[0].strip())
except:
errmsg += "寬度必須為整數!"
try:
height = int(fieldValues[1].strip())
except:
errmsg += "高度必須為整數!"
if errmsg == "":
break
fieldValues = g.multenterbox(errmsg, title, fieldNames, fieldValues)
url = "http://placekitten.com/g/%d/%d" % (width, height)
response = urllib.request.urlopen(url)
cat_img = response.read()
filepath = g.diropenbox("請選擇存放喵的資料夾")
if filepath:
filename = '%s/cat_%d_%d.jpg' % (filepath, width, height)
else:
filename = 'cat_%d_%d.jpg' % (width, height)
with open(filename, 'wb') as f:
f.write(cat_img)
if __name__ == "__main__":
main()
9.登陸豆瓣客戶端
import re
import urllib.request
from http.cookiejar import CookieJar
# 豆瓣的登入url
loginurl = 'https://www.douban.com/accounts/login'
cookie = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor)
data = {
"form_email":"your email",
"form_password":"your password",
"source":"index_nav"
}
data = {}
data['form_email'] = '你的賬號'
data['form_password'] = '你的密碼'
data['source'] = 'index_nav'
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'))
#驗證成功跳轉至登入頁
if response.geturl() == "https://www.douban.com/accounts/login":
html = response.read().decode()
#驗證碼圖片地址
imgurl = re.search('<img id="captcha_image" src="(.+?)" alt="captcha" class="captcha_image"/>', html)
if imgurl:
url = imgurl.group(1)
# 將驗證碼圖片儲存至同目錄下
res = urllib.request.urlretrieve(url, 'v.jpg')
# 獲取captcha-id引數
captcha = re.search('<input type="hidden" name="captcha-id" value="(.+?)"/>' ,html)
if captcha:
vcode = input('請輸入圖片上的驗證碼:')
data["captcha-solution"] = vcode
data["captcha-id"] = captcha.group(1)
data["user_login"] = "登入"
# 提交驗證碼驗證
response = opener.open(loginurl, urllib.parse.urlencode(data).encode('utf-8'))
# 登入成功跳轉至首頁 '''
if response.geturl() == "http://www.douban.com/":
print('登入成功!')