【发布时间】:2018-05-10 10:49:08
【问题描述】:
我正在使用 python、requests 和 BeautifulSoup 构建一个网络抓取应用程序。
我将一个类变量声明为:
class myClass(object):
TAG = "\"span\",{\"data-automation\":\"jobListingDate\"}"
我使用 print self.TAG 验证了这个 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 allhtml.find("span",{"data-automation":"jobListingDate"}) #this line does find what I am after
我一头雾水,self.TAG怎么和"span",{"data-automation":"jobListingDate"}这个字符串不一样,我是不是没有正确转义?
【问题讨论】:
-
什么是
self?html.find是什么? -
@cᴏʟᴅsᴘᴇᴇᴅ,问题已解决。仍然感谢您的评论,我应该从一开始就澄清它们。
标签: python beautifulsoup request