来自 Python 3 的 urllib 用法
urlib usage from Python 3
我收到以下错误:
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
拨打下方电话时
import urllib.request, urllib.parse, urllib.error, urllib.request,
urllib.error, urllib.parse
import json
chemcalcURL = 'http://www.chemcalc.org/chemcalc/em'
# Define a molecular formula string
mfRange = 'C0-100H0-100N0-10O0-10'
# target mass
mass = 300
# Define the parameters and send them to Chemcalc
# other options (mass tolerance, unsaturation, etc.
params = {'mfRange': mfRange,'monoisotopicMass': mass}
response = urllib.request.urlopen(chemcalcURL, urllib.parse.urlencode(params))
# Read the output and convert it from JSON into a Python dictionary
jsondata = response.read()
data = json.loads(jsondata)
print(data)
您必须将您的请求转换为涉及使用 bytes()
参数的字节:
response = urllib.request.urlopen(chemcalcURL, bytes(urllib.parse.urlencode(params), encoding="utf-8")
bytes()
必须采用网站几乎总是 utf-8 的编码。
我收到以下错误:
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
拨打下方电话时
import urllib.request, urllib.parse, urllib.error, urllib.request,
urllib.error, urllib.parse
import json
chemcalcURL = 'http://www.chemcalc.org/chemcalc/em'
# Define a molecular formula string
mfRange = 'C0-100H0-100N0-10O0-10'
# target mass
mass = 300
# Define the parameters and send them to Chemcalc
# other options (mass tolerance, unsaturation, etc.
params = {'mfRange': mfRange,'monoisotopicMass': mass}
response = urllib.request.urlopen(chemcalcURL, urllib.parse.urlencode(params))
# Read the output and convert it from JSON into a Python dictionary
jsondata = response.read()
data = json.loads(jsondata)
print(data)
您必须将您的请求转换为涉及使用 bytes()
参数的字节:
response = urllib.request.urlopen(chemcalcURL, bytes(urllib.parse.urlencode(params), encoding="utf-8")
bytes()
必须采用网站几乎总是 utf-8 的编码。