逃逸的字符串未被识别

2024-09-30 14:18:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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"}不一样,我没有正确地转义什么吗


Tags: 字符串selfdatahtmltaglinefindthis
1条回答
网友
1楼 · 发布于 2024-09-30 14:18:41

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

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

注意字符串周围的单引号',与"\"span\",{\"data-automation\":\"jobListingDate\"}"相同

在第二个例子html.find("span",{"data-automation":"jobListingDate"})中,我们讨论了两个参数

当然,这会有不同的表现

相关问题 更多 >