# 一、构造一个访问阳光电影网的请求(url,headers)
import requests
url='http://www.ygdy8.com'
headers={
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.9',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Cookie':'37cs_pidx=1; 37cs_user=37cs43285651090; 37cs_show=69; UM_distinctid=160bca539501d4-0503c2951b5a5e-57683a71-100200-160bca539517a; CNZZDATA5783118=cnzz_eid%3D55112955-1514992804-http%253A%252F%252Fwww.ygdy8.com%252F%26ntime%3D1514992804',
'Host':'www.ygdy8.com',
'If-Modified-Since':'Wed, 03 Jan 2018 12:57:50 GMT',
'If-None-Match':"0dbc7759284d31:54c",
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'
}
# 二、输出请求的状态码
req=requests.get(url,headers=headers)
status_code=req.status_code
print(status_code)
#运行结果
#200
# 三、输出请求的网页源码
req.encoding='gb2312'
html_content=req.text
print(html_content)
# 四、将源码保存成html文件(文件为'moive.html')
fp=open('movie.html','w',encoding='gb2312')
fp.write(html_content)
fp.close()
网友评论