ajax: 就是一段js代码,通过这段代码,可以让页面发送异步的请求,或者向服务器发送一个东西,即和服务器进行交互
对于ajax:
第二页
第三页就不放了
得到一个规律start =(page-1)*20
import urllib.request
import urllib.parse
# 用函数封装
# 下载豆瓣电影前10页的数据
def create_request(page):
base_url = 'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.200.400 QQBrowser/11.8.5310.400',
}
data = {
'start': (page - 1) * 20,
'limit': 20
}
data = urllib.parse.urlencode(data)
url = base_url + data
# 请求对象定制
request = urllib.request.Request(url, headers=headers)
return request
# 获取响应数据
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
# 下载数据
def down_load(page, content):
with open('douban' + str(page) + '.json', 'w', encoding='utf-8') as fp:
fp.write(content)
if __name__ == '__main__':
start_page = 1
end_page = 10
for page in range(start_page, end_page+1):
request = create_request(page)
content = get_content(request)
down_load(page, content)