Resolving syntax errors

Asked 2 years ago, Updated 2 years ago, 44 views

# -*- coding: utf-8 -*-
from urllib.request import urlopen, Request
import urllib
import bs4

location = 'Long-term'
enc_location = urllib.parse.quote (location + '+weather')

url = 'https://search.naver.com/search.naver?ie=utf8&query='+ enc_location

req = Request(url)
page = urlopen(req)
html = page.read()
soup = bs4.BeautifulSoup(html,'html5lib')
print ('current' + location +' fine dust' + soup.find('dd', class='lv1').find('span', class='num').text + '. ;)

File "", line 15 print ('current' + location +' fine dust' + soup.find('dd', class="lv1")).find('span', class="num").text + '. ;) ^ SyntaxError: invalid syntax

I don't know why syntax is coming up

crawling

2022-09-21 14:47

1 Answers

The keyword class is a reserved word in Python. It should not be used in other situations unless it is included in the string syntax or creates a real class.

There is a reserved word class in a position where parser should not be in code parsing, so you spit out a grammar error.

If you look at the beautiful soup document, The keyword arg that you can put in the find function is class_. You can fix it with this.

https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class


2022-09-21 14:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.