ajax: 就是一段js代碼,通過(guò)這段代碼,可以讓頁(yè)面發(fā)送異步的請(qǐng)求,或者向服務(wù)器發(fā)送一個(gè)東西,即和服務(wù)器進(jìn)行交互
對(duì)于ajax:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-566432.html
- 一定會(huì)有 url,請(qǐng)求方法(get, post),可能有數(shù)據(jù)
- 一般使用 json 格式
- 打開(kāi)豆瓣電影,F(xiàn)12打開(kāi)控制臺(tái)(我這里是科幻類排行榜)
這是第一頁(yè)
第二頁(yè)
第三頁(yè)就不放了
得到一個(gè)規(guī)律start =(page-1)*20文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-566432.html
import urllib.request
import urllib.parse
# 用函數(shù)封裝
# 下載豆瓣電影前10頁(yè)的數(shù)據(jù)
def create_request(page):
base_url = 'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&'
headers = {
'',
}
data = {
'start': (page - 1) * 20,
'limit': 20
}
data = urllib.parse.urlencode(data)
url = base_url + data
# 請(qǐng)求對(duì)象定制
request = urllib.request.Request(url, headers=headers)
return request
# 獲取響應(yīng)數(shù)據(jù)
def get_content(request):
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
return content
# 下載數(shù)據(jù)
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)
到了這里,關(guān)于Python爬蟲(chóng)——urllib_ajax的get請(qǐng)求爬取豆瓣電影前十頁(yè)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!