【Python爬虫】 学习笔记 -- ajax下爬取豆瓣请求第一页

import urllib.request
import urllib.parse

url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'
headers ={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}

# 请求对象的定制
request = urllib.request.Request(url=url,headers=headers)
# 获取响应的数据
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
# 数据下载到本地
# open方法默认情况下gbk的编码,若有中文需手工定义utf-8
fp = open('douban.js','w',encoding='utf-8')
fp.write(content)

 

posted @ 2021-11-05 19:23  Anonytt  阅读(29)  评论(0编辑  收藏  举报