【python】urllib和urllib3,requests 简要概括---笔记

'''
  urllib和urllib3,requests

  url:
  协议://存放资源的地址(域名)/具体的资源
  https://bj.lianjia.com/zufang/dghfjhsjdf648.html

   urllib.request  用来发出请求
   urllib.parse

   urllib.request.urlopen(str)  --->response对象

   request= urllib.request.Request(url,data,headers)
   urllib.request.urlopen(request)  --->response对象

   从response中获取信息
   response.read()   ----> 字节信息

   要对信息进行解码:
   response.read().decode('utf-8')

'''
import urllib.request
import urllib.parse

# response = urllib.request.urlopen('https://bj.lianjia.com/zufang/BJ2319485348977049600.html?nav=0')
url = 'https://bj.lianjia.com/zufang/BJ2319485348977049600.html'

header = {}

header[
    'User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'

data = {}
data['nav'] = 0
data = urllib.parse.urlencode(data).encode('utf-8')

# 创建请求对象
request = urllib.request.Request(url, data, header)
response = urllib.request.urlopen(request)
content = response.read()
# print(content)

print(content.decode('utf-8'))

# urllib3  requests
import urllib3
import requests

# urllib3 requests
# xpath beautifulsoup
# get与post请求   http的请求方式  restful

 

你可能感兴趣的:(Python,基础,爬虫学习)