Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用html.parser_Python_Web Scraping_Python 3.7_Html Parser - Fatal编程技术网

Python 如何使用html.parser

Python 如何使用html.parser,python,web-scraping,python-3.7,html-parser,Python,Web Scraping,Python 3.7,Html Parser,大家好,我是python新手,正在尝试使用python的html.parser模块,我想浏览一下这个网站,用li标记中的html.parser获取URL、交易名称和价格 获取url后,我想将它们附加到基本url中,并从该站点获取具有价格的交易 import urllib.request import urllib.parse import re from html.parser import HTMLParser url = 'https://www.mcdelivery.com.pk/pk/

大家好,我是python新手,正在尝试使用python的html.parser模块,我想浏览一下这个网站,用
li
标记中的html.parser获取URL、交易名称和价格 获取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)

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

import html.parser
类MyParser(html.parser.HTMLParser):
定义初始化(self,html):
self.matches=[]
self.match\u计数=0
super()。\uuuu init\uuuuu()
def句柄_数据(自身、数据):
self.matches.append(数据)
self.match_count+=1
def句柄\u开始标记(自身、标记、属性):
attrs=dict(attrs)
如果标记==“div”:
如果属性获取(“产品成本”):
self.handle_data()
其他:返回
用法大致如下:


request\u html=请求方法(url,…)
parser=MyParser()
提要(请求\ html)
对于parser.matches中的项:
打印(项目)

Python在标准库中有一个模块。您可以随意使用它,直到您得出想要使用BeautifulSoup的结论。
request\u html=the\u request\u方法(url,https://www.mcdelivery.com.pk/pk/browse/menu.html)
我必须这样给出url?
request\u html=the\u request\u方法(url…)
这里应该添加什么
(url…)
url之后?
url=urllib.request.urlopen(“https://www.mcdelivery.com.pk/pk/browse/menu.html)html=url.read().decode()url.close()
我可以这样获取url吗?是的。获取
html
并将其提供给解析器。我正在更新代码,我有一个错误。请帮帮我。错误是MyParser类(html.parser.HTMLParser)中的
第11行:AttributeError:'str'对象没有属性“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)