我正在使用python、requests和BeautifulSoup构建一个web抓取应用程序
我将类变量声明为:
class myClass(object):
TAG = "\"span\",{\"data-automation\":\"jobListingDate\"}"
我使用print self.TAG
验证了这个标记
我从print self.TAG
得到的输出是"span",{"data-automation":"jobListingDate"}
,这表明self.TAG
与这个字符串"span",{"data-automation":"jobListingDate"}
相同
但以下两行代码产生了不同的结果:
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
我很困惑,怎么self.TAG
和这个字符串"span",{"data-automation":"jobListingDate"}
不一样,我没有正确地转义什么吗
在
html.find(self.TAG)
的情况下,实际上只把一个字符串作为参数,即:注意字符串周围的单引号
'
,与"\"span\",{\"data-automation\":\"jobListingDate\"}"
相同在第二个例子
html.find("span",{"data-automation":"jobListingDate"})
中,我们讨论了两个参数当然,这会有不同的表现
相关问题 更多 >
编程相关推荐