Python 无法识别转义字符串

Python 无法识别转义字符串,python,beautifulsoup,request,Python,Beautifulsoup,Request,我正在使用python、requests和BeautifulSoup构建一个web抓取应用程序 我将一个类变量声明为: class myClass(object): TAG = "\"span\",{\"data-automation\":\"jobListingDate\"}" 我使用print self.TAG 我从print self.TAG得到的输出是“span”,“数据自动化”:“jobListingDate”},这表明self.TAG与这个字符串“span”,“数据自动化”

我正在使用python、requests和BeautifulSoup构建一个web抓取应用程序

我将一个类变量声明为:

class myClass(object):
    TAG = "\"span\",{\"data-automation\":\"jobListingDate\"}"
我使用
print self.TAG

我从
print self.TAG
得到的输出是
“span”,“数据自动化”:“jobListingDate”}
,这表明
self.TAG
与这个字符串
“span”,“数据自动化”:“jobListingDate”}

但以下两行代码产生了不同的结果:

  • r=requests.get(“someURL”)
  • html=BeautifulSoup(r.content,“html.parser”)
  • html.find(self.TAG)#此行根本找不到任何内容
  • html.find(“span”,{“数据自动化”:“jobListingDate”})#这行确实找到了我想要的内容
我很困惑,
self.TAG
如何与这个字符串不同
“span”,“data automation”:“jobListingDate”}
,我没有正确地转义任何内容吗?

html.find(self.TAG)
的情况下,实际上只将一个字符串作为参数,即:

html.find('"span",{"data-automation":"jobListingDate"}')
请注意字符串周围的单引号
,它与
“\'span\”,{\'dataautomation\”:\'jobListingDate\”}“

在您的第二个示例
html.find(“span”,{“数据自动化”:“jobListingDate”})
中,我们讨论的是两个参数


当然,这会有不同的表现。

什么是
self
?什么是
html.find
?@cᴏʟᴅsᴘᴇᴇᴅ, 问题解决了。谢谢你的评论,我应该从一开始就解释清楚。谢谢你,我用type()检查html和self.TAG是否属于同一类型,然后我发现了一些错误,正如你所说的。