get请求豆瓣电影 下载
import urllib.parse
import urllib.request
import json
# url = 'https://movie.douban.com/j/chart/top_list?type=19&interval_id=100%3A90&action=&start='+page+'&limit=20'
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
}
def getjson(page):
url = 'https://movie.douban.com/j/chart/top_list?type=19&interval_id=100%3A90&action=&start=' + str(
(page - 1) * 20) + '&limit=20'
requestObj = urllib.request.Request(url=url, headers=headers)
resp = urllib.request.urlopen(requestObj)
context = resp.read().decode('utf-8')
return context
def download(page, context):
with open("豆瓣电影_第" + str(page) + "页.json", 'w', encoding='utf-8') as fp:
fp.write(context)
if __name__ == '__main__':
start_page = int(input("开始页"))
end_page = int(input("结束页"))
for page in range(start_page, end_page + 1):
context = getjson(page)
if context != '[]':
#下载
download(page, context)
标签:__,python,urllib,start,ajax,url,context,page
From: https://www.cnblogs.com/dhcc/p/18295215