|
- #! python3
- import requests
- res = requests.get('https://www.baidu.com/s?wd=python')
- print(res.url)
- print(res.headers)
- print(res.content)
复制代码 爬取这个页面,出来一个莫名其妙的内容.不知道为什么. 代码怎么改?还需要设置什么吗? 多问一句requests.get里 为什么要设置headers,有什么用处.
新手一枚,多多指教
https://www.baidu.com/s?ie=utf-8 ... baidu&wd=python
{'Accept-Ranges': 'bytes', 'Cache-Control': 'no-cache', 'Connection': 'Keep-Alive', 'Content-Length': '227', 'Content-Type': 'text/html', 'Date': 'Tue, 22 Aug 2017 15:14:52 GMT', 'Last-Modified': 'Tue, 08 Aug 2017 12:43:00 GMT', 'P3p': 'CP=" OTI DSP COR IVA OUR IND COM "', 'Pragma': 'no-cache', 'Server': 'BWS/1.1', 'Set-Cookie': 'BD_NOT_HTTPS=1; path=/; Max-Age=300, BIDUPSID=A5F6F7E3ABB0E804ED6D9899C959B3F9; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1503414892; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com', 'Strict-Transport-Security': 'max-age=0', 'X-Ua-Compatible': 'IE=Edge,chrome=1'}
b'<html>\r\n<head>\r\n\t<script>\r\n\t\tlocation.replace(location.href.replace("https://","http://"));\r\n\t</script>\r\n</head>\r\n<body>\r\n\t<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>\r\n</body>\r\n</html>'
红色的为返回的页面
|
|