如何在网站上找到句子

2024-04-25 11:31:14 发布

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

我想做一些简单的单词/句子查找。你知道吗

试过这个:

import urllib
from urllib import request

url = "https://fotka.com/profil/k"
word = "Nie ma profilu"


def search_website(url, word):
page = urllib.request.urlopen(url)
phrase_present = False

for i in page:
    if bytes(word, encoding='utf8') in i:
        phrase_present = True
        print(i)

return phrase_present

finder = search_website(url, word)
print(finder)

看起来效果不错,但是,请解释一下url。 如果在浏览器中打开:

url = "https://fotka.com/profil/k"

确实存在搜索到的word,因此返回True,但如果打开:

url = "https://fotka.com/profil/kkkk"

页面上没有这样的word,它仍然返回True。你知道吗

我检查了变量page的内容,在这两种情况下,它是相同的,而url是不同的。。。你知道吗

有人知道为什么有解决办法的想法吗?你知道吗


Tags: httpsimportcomtrueurlsearchrequestpage
2条回答

如果您的问题是“如何检查页面上是否有可见的测试?”那么,这可能是你的解决方案

import urllib
from bs4 import BeautifulSoup

url = "some page"
word = "some word"

page = urllib.urlopen(url).read()

html = BeautifulSoup(page, "html.parser")
print word in html.get_text()

您发布了一个非常广泛的cast,但我认为您正在寻找段落标记之间的数据<p>

import re
import urllib
url = "some page"
word = "some word"

page_data = str(urllib.urlopen(url).read())
paragraph_data = re.findall("<p>(.*?)</p>", page_data)
final_paragraph_data = [i for i in paragraph_data if word in i]

final_paragraph_data现在存储包含word内容的所有句子簇的列表。你知道吗

相关问题 更多 >