Yu Zhang
Yu Zhang

Reputation: 2149

escaped string is not recognized

I am building a web-scraping application using python, requests and BeautifulSoup.

I declared a class variable as:

class myClass(object):
    TAG = "\"span\",{\"data-automation\":\"jobListingDate\"}"

I verified this TAG by using print self.TAG

and I got the output from print self.TAG is "span",{"data-automation":"jobListingDate"} which suggests self.TAG is the same as this string "span",{"data-automation":"jobListingDate"}

But the following two lines of code produced completed different results:

I am confused, how self.TAG is not the same as this string "span",{"data-automation":"jobListingDate"}, have I not escaped anything properly?

Upvotes: 1

Views: 48

Answers (1)

Mike Scotty
Mike Scotty

Reputation: 10782

In case of html.find(self.TAG) you're actually only putting a single string as a parameter, namely:

html.find('"span",{"data-automation":"jobListingDate"}')

Notice the single quote ' around the string, which is the same as "\"span\",{\"data-automation\":\"jobListingDate\"}"

in your second example html.find("span",{"data-automation":"jobListingDate"}) we're talking about two parameters.

Of course this will behave differently.

Upvotes: 1

Related Questions