Home > Software design >  Python: How to input output specific data into the string?
Python: How to input output specific data into the string?

Time:08-27

I made a code to scrape YouTube video ID's from search with YouTube data API although I need to make another part of code to use those IDs for deeper analysis of content data and retrieve me such data as video description and channel title. How can I improve this code to make 2 part of the script start working correctly and give me back needed data?

Part 1

print(type(youtube))

pp = PrettyPrinter()
nextPageToken = ''

for x in range(1):
#while True:
    request = youtube.search().list(
        q='my unique search query',
        part='id',
#       part='id,snippet',
        maxResults=2,
        order="viewCount",
        pageToken=nextPageToken,
        type='video')
    
    print(type(request))
    res = request.execute()
    pp.pprint(res) 
    
    if 'nextPageToken' in res:
        nextPageToken = res['nextPageToken']
#This code give me relevant ID`s data as the output

Part 2


#Here im trying to input ID`s from output results to the ids 
ids = '(type(request.id))'
results = youtube.videos().list(id=ids, part='snippet').execute()
for result in results.get('items', []):
    print(result['id'])
    print(result ['snippet']['description'])
    print(result ['snippet']['title'])

CodePudding user response:

Just use:

ids = [item['id']['videoId'] for item in res['items']]
  • Related