'How can I return all the titles on the page along with the views, year and channel name?
How can I return all the titles on the page along with the views, year and channel name?
import urllib.request
from bs4 import BeautifulSoup as soup
import pandas as pd
url = "https://www.youtube.com/results?search_query=earpods+pro"
header = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
...
data =[]
def getdata (url):
header = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
req = urllib.request.Request(url, headers=header)
amazon_html = urllib.request.urlopen(req).read()
a_soup = soup(amazon_html,'html.parser')
for e in a_soup.find_all('div',{'id':"contents"}):
try:
title = e.find('h3').text
except:
title = None
data.append({
'title':title
})
return data
The above code is returning no records.
Solution 1:[1]
If you need to parse data from YouTube, you can use YouTube Video Results API from SerpApi. It's a paid API with a free plan. Check out the playground.
The difference is that you don't have to figure out how to create the parser, maintain it, how to scale it, and bypass blocks from Google.
Code example in the online IDE:
from serpapi import GoogleSearch
import os, json
# https://docs.python.org/3/library/os.html#os.getenv
params = {
"api_key": os.getenv("API_KEY"), # your serpapi api key
"engine": "youtube", # search engine
"search_query": "earpods pro" # search query
# other search parameters: https://serpapi.com/youtube-search-api
}
search = GoogleSearch(params)
results = search.get_dict()
video_results = []
for result in results["video_results"]:
video_results.append({
"title": result["title"],
"link": result["link"],
"published_date": result["published_date"],
"views": result["views"],
"length": result["length"],
"description": result["description"]
})
print(json.dumps(video_results, indent=2, ensure_ascii=False))
Part of the output:
[
{
"title": "AirPods Pro - First 11 Things To Do!",
"link": "https://www.youtube.com/watch?v=jWd6gQerRrU",
"published_date": "2 years ago",
"views": 3830507,
"length": "13:44",
"description": "AirPods Pro - First 11 Things To Do! | AirPods Pro Tips & Tricks Did you just get the AirPods Pro? Wondering what to do after ..."
}, ... other results
{
"title": "AirPods Pro Unboxing and Review!",
"link": "https://www.youtube.com/watch?v=s2lCkVHQYus",
"published_date": "2 years ago",
"views": 4519045,
"length": "13:02",
"description": "Testing out the new Apple AirPods Pro! ? SUBSCRIBE FOR MORE VIDEOS: ..."
}
]
Disclaimer, I work for SerpApi.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dmitriy Zub |
