Home Python 3.5 not cooperating with BeautifulSoup and variables
Reply: 1

Python 3.5 not cooperating with BeautifulSoup and variables

Toxxic Published in 2018-01-11 03:03:14Z

I'm trying to write a program that can check whether or not a YouTube video is in the music category or not. I have written some code but its almost as if Python is "lying" to me. Here is the code

>>> from bs4 import BeautifulSoup
>>> from urllib.request import urlopen
>>> url = "https://www.youtube.com/watch?v=LXuNNqoo1WQ"
>>> html = urlopen(url).read()
>>> soup = BeautifulSoup(html, "html.parser")
>>> tags = soup.find_all(class_="content watch-info-tag-list")
>>> hasmusictag = 'href="/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ">Music</a>' in 
>>> print(hasmusictag)
>>> print(tags[0])
<ul class="content watch-info-tag-list">
<li><a class=" yt-uix-sessionlink spf-link " data-
sessionlink="ei=udFWWrG5EYqB8wSag5rgCg" href="/channel/UC-9-

As you can see, hasmusictag should return true, but it is returning false. I have no idea why. All help is appreciated!

Nicholas Flees
Nicholas Flees Reply to 2018-01-11 03:58:11Z

I don't think @Jay is trying to tell you that the in operator will raise an exception, but rather that you are expecting the in operator to work in the same way it works for a string. In this case, tags[0] is an instance of the bs4.element.Tag class and, evidently, the in operator (by virtue of the __contains__ operator overloading method) is not implemented this way.

There are a number of ways to go about making this work the way you want, but maybe the simplest would be to write the following:

hasmusictag = 'href="/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ">Music</a>' in str(tags[0])
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305063 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO