示例:(百度翻译) post_url = 'http://fanyi.baidu.com/sug' wd = input("请输入英语单词:") #post的参数,写成字典的形式 data = {'kw':wd} #对表单数据进行处理,先转换为字符串 再转换为字节格式 data = urllib.parse.urlencode(data).encode("utf8") #构建请求对象 headers={ request = urllib.request.Request(post_url=url,headers=headers) #获取请求的对象(如果请求方式不是post不要加data) response = urllib.request.urlopen(request,data=data) #存储文件 with open('文件存储路径','wb') as fp: fp.write(response.read()) |
|