python 学习笔记urllib

# *-* encoding:utf-8 -*-
# 
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com',data=data)
print(response.read().decode())

data 参数

import urllib.request,urllib.parse
data = bytes(urllib.parse.urlencode({'wd':'hello'}),encoding='utf-8')
response = urllib.request.urlopen('https://www.***.com/s?', data=data,timeout=1)
print(response.read().decode())

header参数

import urllib.request
url ='http://www.baidu.com/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request).read().decode()
print(response)

 

This function always returns an object which can work as a context manager and has methods such as

  • geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
  • info() — return the meta-information of the page, such as headers, in the form of an email.message_from_string() instance (see Quick Reference to HTTP Headers)
  • getcode() – return the HTTP status code of the response.
import urllib.request
url ='https://blog.csdn.net/duxu24/article/details/77414298'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
# print(response.read().decode())
print(response.getcode())
print(response.info())
print(response.geturl())

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值