#!/usr/bin/python
# coding=utf-8
# 作者 :Y0010026
# 創建時間 :2018/12/16 16:27
# 文件 :spider_05.py
# IDE :PyCharm
import urllib2
import urllib
url = 'https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10'
# 要傳遞的post方式的數據,有可能會有多組數據
submit_data = {
'start': 20,
'tags': '喜劇'
}
# 編碼
data = urllib.urlencode(submit_data)
# 構造請求頭,創建請求對象
headers = {
"Accept": "application/json,text/plain,*/*",
"User-Agent": "Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1",
"Accept-Language": "zh-CN,zh;q=0.8"
}
requset = urllib2.Request(url, data=data, headers=headers)
# 發送請求,獲取服務器響應數據
response = urllib2.urlopen(requset)
# 獲取爬取到的數據
content = response.read()
# 保存數據
with open('movies.json', 'w') as f:
f.write(content)