Reputation: 2149
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:
r = requests.get("someURL")
html = BeautifulSoup(r.content, "html.parser")
html.find(self.TAG) #this line does not find anything at all
html.find("span",{"data-automation":"jobListingDate"}) #this line does find what I am after
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
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