而不是像这样放置一些标头位置:
header("Location: pagename.php?alert=xxxx");
我想在当前打开的页面上添加?alert = xxx,怎么做?
答案 0 :(得分:1)
尝试使用import scrapy
import urllib
import json
import datetime
from youtube_scrapy.items import YoutubeItem
class YoutubeSpider(scrapy.Spider):
name = 'youtube'
BASE_URL = 'https://www.googleapis.com/youtube/v3'
def __init__(self):
self.access_key = 'you_yuotube_api_access_key'
self.video_id = 'any_youtube_video_id'
def start_requests(self):
params = {
'key': self.access_key,
'part': 'snippet,replies',
'videoId': self.video_id,
'maxResults': 100
}
url = f'{self.BASE_URL}/commentThreads/?{urllib.parse.urlencode(params)}'
request = scrapy.Request(url, callback=self.parse)
request.meta['params'] = params
return [request]
def parse(self, response):
data = json.loads(response.body)
# lets collect comment and reply
items = data.get('items', [])
for item in items:
created_date = item['snippet']['topLevelComment']['snippet']['publishedAt']
_created_date = datetime.datetime.strptime(created_date, '%Y-%m-%dT%H:%M:%S.000Z')
id = item['snippet']['topLevelComment']['id']
record = {
'created_date': _created_date,
'body': item['snippet']['topLevelComment']['snippet']['textOriginal'],
'creator_name': item['snippet']['topLevelComment']['snippet'].get('authorDisplayName', {}),
'id': id,
'url': f'https://www.youtube.com/watch?v={self.video_id}&lc={id}',
}
yield YoutubeItem(**record)
# lets paginate if next page is available for more comments
next_page_token = data.get('nextPageToken', None)
if next_page_token:
params = response.meta['params']
params['pageToken'] = next_page_token
url = f'{self.BASE_URL}/commentThreads/?{urllib.parse.urlencode(params)}'
request = scrapy.Request(url, callback=self.parse)
request.meta['params'] = params
yield request
PHP_SELF