【发布时间】:2015-11-11 14:30:46
【问题描述】:
首先:我在循环中打开一些 url 并获取 html 代码的标题和描述。 (作品)
第二:我想检查标题或描述中是否有给定的字符串(关键字)。 (不工作)
# 2 - Beautifulsoup: Soll Keywords suchen und Schema.org
kw = "optiker"
seite = obj_det.get('result',{}).get('website')
try:
req = urllib.request.urlopen(seite).read()
soup = BeautifulSoup(req, "html.parser")
desc = soup.findAll(attrs={"name":"description"})
title = soup.title.string
except:
title = "Zugriff verweigert"
desc = "Zugriff verweigert"
kwindesc = kw in desc
kwintitle = kw in title
print(title)
print(desc)
print(kwindesc)
print(kwintitle)
print("-----")
我的屏幕上总是显示 False。但肯定有标题带有字符串“optiker”。
【问题讨论】:
-
desc和title的类型是什么?它是某种节点对象吗? -
我不知道。我在学习python方面很新。我试过了
kwindesc = kw in str(desc)。也不行。 -
你能给我们一个你试图抓取的页面的例子吗?
-
您可以使用
type()函数检查对象的类型。print( type(desc) )报告什么? -
www[.]optiker-bode[.]de - 看到标题有字符串“optiker”。所以我应该有一个真实的。