1. 程式人生 > 其它 >爬蟲庫urllib的使用

爬蟲庫urllib的使用

  

# -*- coding: utf-8 -*-
# @Time : 2021/7/18 11:40
# @Author :liuw
# @File : testUrllib.py
# @Software: PyCharm

import urllib.request
import urllib.parse

# 獲取get請求
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8')) # 建議對返回到的網頁原始碼進行解碼

# 獲取post請求 傳送一個表單資料(把使用者名稱和密碼進行加密傳輸)模擬使用者登入,現實可能還會攜帶cookie資訊
# 介紹一個網站 http://httpbin.org 當你向其傳送訪問時,它會告訴你得到什麼響應結果 測試http和https
# A simple HTTP Request & Response Service.
# data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8") #post封裝的data
# response = urllib.request.urlopen("http://httpbin.org/post",data=data)
# print(response.read().decode('utf-8')) # 該資料和網站執行post請求得到的資訊是一致的

#得到的資料 "User-Agent": "Python-urllib/3.8", 告訴網站我們是爬蟲程式,真實的瀏覽器訪問得到的User-Agent為
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",

# 超時處理機制 0.01s是模擬網站實際訪問的超時處理機制時間
# try:
# response = urllib.request.urlopen("http://httpbin.org/get",timeout=0.01)
# print(response.read().decode('utf-8'))
# except urllib.error.URLError as e:
# print('time out!')


# response = urllib.request.urlopen("http://douban.com")
# print(response.status) # 報418 發現你是爬蟲程式

#拿到server屬性
# response = urllib.request.urlopen("http://baidu.com")
# print(response.getheader("Server"))

'''
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Content-Length": "0",
"Host": "httpbin.org",
"Origin": "http://httpbin.org",
"Referer": "http://httpbin.org/",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-60f3a4ea-5c26c4624ab4486627dc377e"
},
"json": null,
"origin": "113.116.176.73",
"url": "http://httpbin.org/post"
}
'''
#偽裝成瀏覽器去訪問豆瓣,也可以攜帶一些資料,headers 以及method方式
# 如何通過瀏覽器找到User-Agent 訪問百度地址,紅燈點開重新整理百度網址,然後在點選停止紅燈:https://jingyan.baidu.com/article/95c9d20d7bca17ec4e7561a4.html
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
# url = "http://httpbin.org/post"
# data = bytes(urllib.parse.urlencode({"name":"eric"}),encoding="utf-8") #post封裝的data
# # 構建一個請求物件 包含請求的頭部資訊、data、url
# req = urllib.request.Request(url=url,data=data,headers=headers,method="POST")
# response = urllib.request.urlopen(req)
# print(response.read().decode('utf-8'))

# 模擬瀏覽器去訪問豆瓣網站
url="http://douban.com"
# 構建一個請求物件 包含請求的頭部資訊、data、url
req = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))



我希望有個如你一般的人, 如山間清爽的風, 如古城溫暖的光, 只要最後是你就好。 今天, 你路過了誰? 誰又丟失了你呢?