Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Syntax error in For loop because of searching for a ":" in Python

This is probably a really easy fix but I don’t know how to do it. I’m using Python.

from bs4 import BeautifulSoup as beauty
import cloudscraper


scraper = cloudscraper.create_scraper(delay=10, browser='chrome') #
url = ""

info = scraper.get(url).text
soup = beauty(info, "html.parser")
description = soup.find_all('meta', property_ = 'og:title')

for og:title in description:
    print(og:title.text)  

I just want to search for og:title in description but the : in og:title keeps giving me a syntax error. I tried quotation marks but that didn’t work. I had the same problem with a – sign in another thing I was searching for. How do I make python treat them as just text? Sorry for bad question this is my first day ever coding

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

for og:title in description:
    print(og:title.text)  

That’s not valid python code.
Identifiers should contain alphanumeric
and _ underscore characters.
Avoid putting : colon in an identifier.

Spell it this way and you should be fine:

for og_title in description:
    print(og_title.text)  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading