使用python从网站页面查找特定单词

2024-04-27 05:08:08 发布

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

theurl = "https://www.facebook.com/bbcnews"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage,"html.parser")
body_elem = soup.findAll('div',{"class":"_1xnd"})
for word in body_elem:
    c= word.text
    if c == "BBC":
        print(c)
    else:
        print("unable to find the element")

我想从页面中找到一个特定的单词,因此为了缩小搜索范围,我先用“class”找到“div”,然后找到在该“div”中找到的所有文本,然后将给定的单词(在我的例子中是“BBC”)与body中找到的单词进行匹配。elem然后打印出“BBC”,但它没有得到“BBC”这个单词,而是打印出“else”部分


Tags: httpsdivfacebookwwwbody单词elsebbc