一、获取网页内容
分析: res = requests.get(“http://www.baidu.com“) res.text返回的是Unicode型的数据。 使用res.content返回的是bytes型的数据。 也就是说,如果你想取文本,可以通过res.text。 如果想取图片,文件,则可以通过res.content。 方法1:使用res.content,得到的是bytes型,再转为str url='http://news.baidu.com' res = requests.get(url) html=res.content html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore") print(html_doc) 方法2:使用res.text url="http://news.baidu.com" res=requests.get(url) res.encoding='utf-8' print(res.text) 方法3:使用res.text+apparent_encoding url="http://news.baidu.com" res=requests.get(url) res.encoding=res.apparent_encoding print(res.text)
二、获取内容后存入本地
方法1:r.content为bytes型,则open时需要open(filename,”wb”) res=requests.get("music.baidu.com") html=res.content withopen('test.html','wb') as f: f.write(html) 方法2:r.content为bytes型,转为str后存储 res = requests.get("http://www.baidu.com") html=res.content html_doc=str(html,'utf-8') #html_doc=html.decode("utf-8","ignore") withopen('test5.html','w',encoding="utf-8") as f: f.write(html_doc) 方法3:r.text为str,可以直接存储 res=requests.get("http://www.baidu.com") res.encoding='utf-8' html=res.text withopen('test.html','w',encoding="utf-8") as f: f.write(html)