Python 爬虫 urllib.request 对比 requests

get请求对比:  get请求相对urllib.rqquest      则没有需要特别的转码就可以得到响应的数据,非茶馆的方便 , requests中只需要  .text 属性就能获取到源码,而urllib.request.urlopen()之后还得.read().decode('utf-8')去解码才能获取到解码后的源码,很不友好。

# requests   代码
import requests

url = "https://www.baidu.com/s?"

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

datas = {
    'wd':'北京'
}
response = requests.get(url=url,params=datas,headers=headers)

content = response.text
with open("北京.html","w",encoding='utf-8') as op:
    op.write(content)
print(content)
import urllib.request
import urllib.parse

base_url = "https://www.baidu.com/s?"

data = {
    'wd':'周杰伦',
    'sex':'男',
    'location':'中国台湾省'
}
new_date = urllib.parse.urlencode(data)
base_url+=new_date

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
request = urllib.request.Request(url=base_url,headers=headers);
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)

post 请求对比:

# 对比urllib.request
# 优点:  1.post请求不需要编解码    2.post请求参数是data   3.不需要请求对象的定制
import requests
import json

url = "https://fanyi.baidu.com/sug"

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

datas = {
    'kw':'ever'
}

# 对比urllib.request
# 优点:  1.post请求不需要编解码    2.post请求参数是data   3.不需要请求对象的定制
response = requests.post(url=url,headers=headers,data=datas)

# content = response.text
# print(content)
content = response.text
print(content)
print("------")
obj = json.loads(content)
print(obj)

猜你喜欢

转载自blog.csdn.net/weixin_46310452/article/details/126004939
今日推荐