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)