urllib

import urllib.request
import urllib.parse

# 请求地址
url = 'http://www.baidu.com'
url_img_video = '' # 影音文件

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

# 请求参数
data = {
    'kw':'',
    'username':''
}
# 对请求参数编码
# new_data_get = urllib.parse.urlencode(data) # get请求
new_data_post = urllib.parse.urlencode(data).encode('utf-8') # post请求

# 代理池
proxies_pool = [
    {'http':'118.24.219.151:16871'},
    {'http':'118.24.219.151:16871'}
]
import random
proxies = random.choice(proxies_pool)


# 1.直接请求地址(get请求)
# response = urllib.request.urlopen(url)
# content = response.read().decode('utf-8') # 读取所有数据
# print(content)

# response = urllib.request.urlretrieve(url=url_img_video,filename='这里写下载后的文件名,要有后缀名') # 文件下载

# 2.添加请求头等,如果是get请求,直接将new_data拼接到URL上;如果是post请求则需要添加"data = "
# request = urllib.request.Request(url=url + new_data_get,headers=headers) # get请求
# request = urllib.request.Request(url=url,headers=headers,data=new_data_post) # post请求
# response = urllib.request.urlopen(request)
# content = response.read().decode('utf-8')
# print(content)

# 3.处理器处理请求
request = urllib.request.Request(url=url,headers=headers)
# handler = urllib.request.HTTPHandler() # 无代理
handler = urllib.request.ProxyHandler(proxies=proxies) # 有代理
opener = urllib.request.build_opener(handler)
response = opener.open(request)
content = response.read().decode('utf-8')

# 4.返回的数据显示成json格式
import json
obj = json.loads(content)
print(obj['data'])

 

posted @ 2024-01-30 09:11  admin_jh  阅读(9)  评论(0编辑  收藏  举报