直播平台源代码,Scrapy抓取滚动加载的页面
直播平台源代码,Scrapy抓取滚动加载的页面
编写spider
打开qqmovie.py
import scrapy
from movies.items import MoviesItem
class QqmovieSpider(scrapy.Spider):
name = 'qqmovie'
allowed_domains = ['v.qq.com']
# 抓取开始的URL
start_urls = ['https://v.qq.com/channel/movie?listpage=1&channel=movie&itype=100004']
# 偏移标志位
offset = 0
def parse(self, response):
# 通过class=list_item的div标签提取数据
movies = response.xpath('//div[@class="list_item"]')
for i in movies:
item = MoviesItem()
# 提取<a>标签的title作为名称
item['name'] = i.xpath('./a/@title').get()
# 提取<div>标签的title作为主演
item['starring'] = i.xpath('./div/div/@title').get()
yield item
mod_page = response.xpath('//div[@class="mod_pages"]')
# 按下一页按钮的正常class提取,提取不到说明翻页到头了
next_page = mod_page.xpath('//button[@class="page_next "]').extract_first('not found')
if next_page != 'not found':
# 原有网页的逻辑是一页30条数据
self.offset += 30
url = 'https://v.qq.com/x/bu/pagesheet/list?append=1&channel=movie&itype=100004&listpage=2&offset={}&pagesize=30'.format(str(self.offset))
yield scrapy.Request(url=url, callback=self.parse)
else:
print('数据抓取完毕')
以上就是 直播平台源代码,Scrapy抓取滚动加载的页面,更多内容欢迎关注之后的文章