终端输入保存到文件中

import urllib.request
import urllib.parse

#从终端输入直接获取数据,保存到本地

country = input('请输入国家')
url = 'http://www.baidu.com/s?'

data = {
'ie':'utf-8',
'wd':country,
}

url += urllib.parse.urlencode(data)
print(url)

# response = urllib.request.urlopen(url)

filename = country +'.html'

# with open(filename,'wb') as fp :
#   fp.write(response.read())

#代码简洁,直接将保存到本地
urllib.request.urlretrieve(url,filename)

猜你喜欢

转载自www.cnblogs.com/airapple/p/9131827.html