关于urllib.request.Request的理解

#! usr/bin/python3.5
# -*- coding:utf -8 -*-

import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
url = 'https://www.baidu.com/'
headers = {'user_agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', \
           'Host' : 'www.baidu.com'}
headers['Host'] = 'www.baidu.com'
dict = {'name' : 'zh-CN'}

data = urllib.parse.urlencode(dict).encode('utf-8')
#data参数如果要传必须传bytes(字节流)类型的,如果是一个字典,先用urllib.parse.urlencode()编码。
request = urllib.request.Request(url = url, data = data, headers = headers, method = 'GET')
response = urllib.request.urlopen(request)
htmlPage = response.read().decode('utf-8')
soup = BeautifulSoup(htmlPage, 'html.parser')
print(soup.contents)

 

你可能感兴趣的:(Python学习)