【问题标题】:How to use html.parser如何使用 html.parser
【发布时间】:2020-06-10 20:11:10
【问题描述】:

大家好,我是 python 新手,正在尝试使用 python 的 html.parser 模块,我想抓取这个网站并使用 li 标签内的 html.parser 获取 URL、交易名称和价格 https://www.mcdelivery.com.pk/pk/browse/menu.html 获取 url 后,我想将它们附加到基本 URL 中,并从该站点获取带有价格的交易。

import urllib.request
import urllib.parse
import re
from html.parser import HTMLParser

url = 'https://www.mcdelivery.com.pk/pk/browse/menu.html'
values = {'daypartId': '1', 'catId': '1'}
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')  # data should be bytes
req = urllib.request.Request(url, data)
resp = urllib.request.urlopen(req)
respData = resp.read()
list1 = re.findall(r'<div class="product-cost"(.*?)</div>', str(respData))
for eachp in list1:
    print(eachp)

正在使用正则表达式来获取课程,但我失败了。现在试图弄清楚如何使用 html.parser 来完成它。我知道使用beautifulsoup and scrapy 会使工作变得更容易,但我正在尝试使用裸 python,所以请跳过第 3 方库。我真的需要帮助。我被困住了。 Html.parser 代码(更新)

from html.parser import HTMLParser
import urllib.request
import html.parser
# Import HTML from a URL
url = urllib.request.urlopen(
    "https://www.mcdelivery.com.pk/pk/browse/menu.html")
html = url.read().decode()
url.close()


class MyParser(html.parser.HTMLParser):
    def __init__(self, html):
        self.matches = []
        self.match_count = 0
        super().__init__()

    def handle_data(self, data):
        self.matches.append(data)
        self.match_count += 1

    def handle_starttag(self, tag, attrs):
        attrs = dict(attrs)
        if tag == "div":
            if attrs.get("product-cost"):
                self.handle_data()
            else:
                return

parser = MyParser(html)
parser.feed(html)

for item in parser.matches:
    print(item)

【问题讨论】:

  • Python 在标准库中有一个html.parser 模块。随意使用它,直到您得出要使用 BeautifulSoup 的结论。

标签: python web-scraping python-3.7 html-parser


【解决方案1】:

这是一个可能需要特定调整的良好开端:

import html.parser

class MyParser(html.parser.HTMLParser):

    def __init__(self, html):
        self.matches = []
        self.match_count = 0
        super().__init__()        

    def handle_data(self, data):
        self.matches.append(data)
        self.match_count += 1

    def handle_starttag(self, tag, attrs):
        attrs = dict(attrs)
        if tag == "div":
            if attrs.get("product-cost"):
                self.handle_data()
        else: return

用法大致如下

request_html = the_request_method(url, ...)

parser = MyParser()
parser.feed(request_html)

for item in parser.matches:
    print(item)

【讨论】:

  • request_html = the_request_method(url, https://www.mcdelivery.com.pk/pk/browse/menu.html)我必须这样提供网址?
  • request_html = the_request_method(url, ...)这里应该在url后面加上(url, ...)
  • url = urllib.request.urlopen( "https://www.mcdelivery.com.pk/pk/browse/menu.html") html = url.read().decode() url.close() 我可以这样获取网址吗?
  • 对,对。获取html 并将其提供给解析器。
  • 我正在更新代码,我遇到了错误。请帮我。错误是line 11, in &lt;module&gt; class MyParser(html.parser.HTMLParser): AttributeError: 'str' object has no attribute 'parser'
猜你喜欢
  • 2023-01-18
  • 2016-10-22
  • 2016-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2019-02-17
  • 2013-04-28
相关资源
最近更新 更多