找不到python html解析器数据

找不到python html解析器数据,python,html,parsing,web-scraping,python-requests,Python,Html,Parsing,Web Scraping,Python Requests,因此,我正在制作一个网页“爬虫”,它解析一个网页,然后在网页中搜索一个单词或一组单词。这里出现了我的问题,我正在寻找的数据包含在已解析的网页中(我使用特定的单词作为测试运行它),但它表示尚未找到它正在寻找的数据 from html.parser import HTMLParser from urllib import * class dataFinder(HTMLParser): def open_webpage(self): import urllib.request

因此,我正在制作一个网页“爬虫”,它解析一个网页,然后在网页中搜索一个单词或一组单词。这里出现了我的问题,我正在寻找的数据包含在已解析的网页中(我使用特定的单词作为测试运行它),但它表示尚未找到它正在寻找的数据

from html.parser import HTMLParser
from urllib import *

class dataFinder(HTMLParser):
    def open_webpage(self):
        import urllib.request
        request = urllib.request.Request('https://www.summet.com/dmsi/html/readingTheWeb.html')#Insert Webpage
        response = urllib.request .urlopen(request)
        web_page = response.read()
        self.webpage_text = web_page.decode()
        return self.webpage_text


    def handle_data(self, data):
        wordtofind = 'PaperBackSwap.com'
        if data == wordtofind:
            print('Match found:',data)
        else:
            print('No matches found')



p = dataFinder()
print(p.open_webpage())
p.handle_data(p.webpage_text)
我使用feed方法在没有打开网页功能的情况下运行了这个程序,它可以工作并找到数据,但是现在不工作了


非常感谢您在解决此问题时提供的任何帮助

您正在尝试比较html页面和字符串,当然它们并不相似,因此您得到了“未找到匹配项”。要在字符串中查找字符串,可以使用方法。它返回文本else-1的第一个找到位置的位置

正确代码:

from html.parser import HTMLParser
from urllib import *

class dataFinder(HTMLParser):
    def open_webpage(self):
        import urllib.request
        request = urllib.request.Request('https://www.summet.com/dmsi/html/readingTheWeb.html')#Insert Webpage
        response = urllib.request .urlopen(request)
        web_page = response.read()
        self.webpage_text = web_page.decode()
        return self.webpage_text

    def handle_data(self, data):
        wordtofind = 'PaperBackSwap.com'
        if data.find(wordtofind) != -1:
            print('Match found position:', data.find(wordtofind))
        else:
            print('No matches found')

p = dataFinder()
print(p.open_webpage())
p.handle_data(p.webpage_text)

我可以用Beautifulsoup解析和查找html内容中的文本,请查看它是否适用于您。下面是您案例的示例代码

from bs4 import BeautifulSoup

soup= BeautifulSoup(web_page,'html.parser')
for s in soup.findAll(wordtofind):
    if data == wordtofind:
        print('Match found:',data)
    else:
        print('No matches found')
这会让你的生活轻松很多

import requests
from html.parser import HTMLParser

class dataFinder(HTMLParser):
    def open_webpage(self):
        request = requests.get('https://www.summet.com/dmsi/html/readingTheWeb.html')
        self.webpage_text = request.text
        return self.webpage_text

你到底想从网站上提取什么?来自href标签的链接?我只是想从页面中查找文本,无论是在href标签中还是在p标签中。这确实有效,我必须感谢您向我介绍这一点。我对编程非常陌生,因此没有机会深入研究文档,如果有人能告诉我文档中的位置,我将不胜感激。你还说它返回第一个找到的位置,有没有办法让它返回word@S0lo您可以使用此函数-获取子字符串的所有位置。您可以这样使用它:
allindices(data,wordtofind)