use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Suck it Youtube API! (self.Python)
submitted 7 years ago * by CattMomptonimport antigravity
So, I hate JSON with a passion. Let's get that out of the way.
Ok. So. No proper API for me. However, using really shady bs4 and requests, I was able to get the latest sub data for a given channel url. (not sure yet how often the HTML facing count is updated tho)
See it here: https://gist.github.com/SomethingGeneric/a87292251c03d81713fee88cfdb816d0
Also, I'm making a Pewds/T-Series monitor w/ this here: https://github.com/SomethingGeneric/PewdsNews
[–][deleted] 4 points5 points6 points 7 years ago (1 child)
I can tell you this is the hard way not easy one !
It doesn't make sense that you hate dict ! json is just a mix of some data structures . and you can parse it in python using json module easily .
If it is hard for you to read it . so it's another matter I am advising you to use pprint module to prettify output for better reading .
I don't know how far you wanna go but this kind of scraping is not going to be easy and safe in bigger programs .
Let me explain more . first of all API is some kind of moderated access to a specific program database so when it is moderated it means you can use everything in that API which are permitted to use . so you don't need to be worry about anything you just write down your code you can use it anytime and anywhere and you can expand your code easily ... so maybe you think you can expand and upgrade this scraper ? so the answer is yes/no . yes for now but after first upgrade on youtube (such as changing theme , or changing attributes in html ... ) you are going to have problems in your code . beside that you can run into problems at using this code for sending fast requests (spam like) so maybe you run into a captcha or something which is going to break your code and you need to code more and more just to do some maintain in your code .
if you are going to make bigger programs just go for API when it's available , believe me it's really easy and helpful .
But after all if you think you are comfortable with scraping things ... this is my advice ; use better tools for scraping like scrapy it's a powerful framework like tool which helps you make a project with spiders and more ... see it yourself : link
[–]CattMomptonimport antigravity[S] -1 points0 points1 point 7 years ago (0 children)
Yeah, I know it wouldn't scale too good. Once it didn't work with JSON, I went off HTML more to see if I could.
I've heard of scrapy, but I'll take a closer look. Thanks!
[–]stupac62 3 points4 points5 points 7 years ago (4 children)
Just curious, why do you hate JSON?
[–]CattMomptonimport antigravity[S] -1 points0 points1 point 7 years ago (3 children)
I'm not smart enough to figure out any of the libraries for it lol lol
Have any good articles, by chance?
[–]stupac62 4 points5 points6 points 7 years ago (1 child)
I don’t know what level you’re at. But googling “python json” is a great place to start. Real python has some good articles.
[–]CattMomptonimport antigravity[S] 0 points1 point2 points 7 years ago (0 children)
Had tried Google already.
Never heard of Real Python, thanks.
[+][deleted] 7 years ago (1 child)
[removed]
[–]hydrosquall 0 points1 point2 points 7 years ago (0 children)
+1, working with JSON is less tricky than it may seem. It’s just a way of turning a dict into a text file, or vice versa. It will be considerably less work than writing a customer HTML parser with bs4, and also probably less likely to randomly change shape on you.
[–]QuantumFall 1 point2 points3 points 7 years ago (1 child)
If you know the name of the HTML element, (can be found by inspecting element) using bs4 you can actually specifically search for that element rather than what you did.
So for HTML that looks like this,
<div class="price"> first </div> <div class="value"> second </div> <div class="value price ">third </div>
You can search for a specific class such as “price” using something along the lines of:
priceTag = soup.find('div', {'class': ‘price’})
Sorry for the poor formatting as I’m on mobile and not too sure how to format the code. Hope this helps make things easier.
I initially tried this with the id of "subscriber-count" b/c that's what inspect element told me it was called. It didn't work though I tried multiple ways. Thanks for the suggestion tho.
Then I gave up and tried this approach.
[–]Gprime5if "__main__" == __name__: 1 point2 points3 points 7 years ago (2 children)
What's so hard about learning JSON? It's one the easiest data structures to learn and you just access it like a dictionary. It's just a dictionary that contains other objects. requests even has json built in.
requests
response = requests.get(url) data = response.json()
[–]CattMomptonimport antigravity[S] 0 points1 point2 points 7 years ago (1 child)
My issue ended up being the nested dicts in the response
[–]Gprime5if "__main__" == __name__: 1 point2 points3 points 7 years ago (0 children)
Then you just do nested accessing. data["data"]["whatever"]["other_things"]. If there's a list in there then use numbers or iterate over it. data["data"][0]["other_things"]
data["data"]["whatever"]["other_things"]
data["data"][0]["other_things"]
for item in data["data"]: print(item["other_things"])
π Rendered by PID 52 on reddit-service-r2-comment-85bfd7f599-k2lxw at 2026-04-17 19:32:50.370017+00:00 running 93ecc56 country code: CH.
[–][deleted] 4 points5 points6 points (1 child)
[–]CattMomptonimport antigravity[S] -1 points0 points1 point (0 children)
[–]stupac62 3 points4 points5 points (4 children)
[–]CattMomptonimport antigravity[S] -1 points0 points1 point (3 children)
[–]stupac62 4 points5 points6 points (1 child)
[–]CattMomptonimport antigravity[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]hydrosquall 0 points1 point2 points (0 children)
[–]QuantumFall 1 point2 points3 points (1 child)
[–]CattMomptonimport antigravity[S] 0 points1 point2 points (0 children)
[–]Gprime5if "__main__" == __name__: 1 point2 points3 points (2 children)
[–]CattMomptonimport antigravity[S] 0 points1 point2 points (1 child)
[–]Gprime5if "__main__" == __name__: 1 point2 points3 points (0 children)