python爬虫-爬取豆瓣电影数据

时间:2022-11-17 15:26:53
#!/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)