【问题标题】:python html parser data not being found找不到python html解析器数据
【发布时间】:2017-08-14 10:14:02
【问题描述】:

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

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 方法在没有打开网页功能的情况下运行了程序,它可以工作并找到数据,但是现在它不工作了。

感谢您对解决此问题的任何帮助

【问题讨论】:

  • 您打算从网站中提取的究竟是什么?来自 href 标签的链接?
  • 我只是想从页面中查找文本,无论是在 href 标签中还是在 p 标签中

标签: python html parsing web-scraping python-requests


【解决方案1】:

您正在尝试比较 html 页面和字符串,当然它们并不相似,因此您得到“未找到匹配项”。要在字符串中查找字符串,您可以使用str.find() 方法。它返回第一个找到的文本位置的位置 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)

【讨论】:

  • 这确实有效,我必须感谢您向我介绍这一点。我对编程很陌生,因此没有机会非常彻底地探索文档,如果有人能指出我在文档中的哪个位置,那么我将非常感激。你还说它返回第一个找到的位置,有没有办法让它返回单词的所有位置
  • @S0lo 您可以使用此函数 - code.activestate.com/recipes/… 获取子字符串的所有位置。你可以这样使用它:allindices(data, wordtofind)
【解决方案2】:

我可以使用 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')

【讨论】:

    【解决方案3】:

    Late to the party, but I would strongly advise using the requests module for HTTP interactions.它会让你的生活更轻松。

    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
    

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 2019-03-02
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多