1. 程式人生 > >爬蟲2 urllib用法

爬蟲2 urllib用法

from urllib import request,parse

# 1.    解析資料
# 解析一條
# response = request.urlopen(url='http://httpbin.org/get?username={}'.format(parse.quote('哈哈')))
# print(response.read())
# # 解析字典
# data1 = {
#     'username':'哈哈',
#     'pass' : 23564
# }
# res = parse.urlencode(data1)
# print(res)


# 2.    模擬請求頭
# headers = { # 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6721.400 QQBrowser/10.2.2243.400' # } # req = request.Request( # url='http://img5.imgtn.bdimg.com/it/u=935292084,2640874667&fm=26&gp=0.jpg', # headers=headers
# ) # response = request.urlopen(req) # print(response.read()) # 3. 傳送一個get請求 # response = request.urlopen(url='http://httpbin.org/get',timeout=0.1) # 傳送一個get請求 # response = request.urlopen(url='http://httpbin.org/get') # print(response.getcode()) #返回碼 # print(response.info()) #返回頭部資訊
# print(response.read()) #返回內容 # response2 = request.urlopen(url='http://httpbin.org/post', # data = b'username=haha&password=12345'