Skip to content
Advertisement

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

Advertisement

Answer

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)  
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement