为了账号安全,请及时绑定邮箱和手机立即绑定

如何在python中添加'features="html.parser"'

如何在python中添加'features="html.parser"'

神不在的星期二 2021-11-09 15:37:54
我有一些返回错误的代码:UserWarning:没有明确指定解析器,所以我使用了这个系统最好的可用 HTML 解析器(“html.parser”)。这通常不是问题,但如果您在另一个系统上或在不同的虚拟环境中运行此代码,它可能会使用不同的解析器并表现出不同的行为。导致此警告的代码位于文件 scrape7.py 的第 14 行。要消除此警告,请将附加参数 'features="html.parser"' 传递给 BeautifulSoup 构造函数。我的代码是:import numbersimport httplib2import requestsimport refrom bs4 import BeautifulSoup, SoupStrainerhttp = httplib2.Http()status, response = http.request('https://www.macys.com/social/the-edit/')editurlslist = []for link in BeautifulSoup(response, parse_only=SoupStrainer('a')):    if link.has_attr('href'):        if '/the-edit' in link['href']:            editurlslist.append(str("https://www.macys.com"+link['href']))products = []for i in editurlslist:   products.append(re.findall(r'(data-thisProduct=\"[0-9]{7}\")',    requests.get(i).text))for i in editurlslist:   products.append(re.findall(r'(productid=\"[0-9]{7}\")',    requests.get(i).text))products2 = [x for x in products if type(x) in numberic_types]print(products2)
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

将“html.parser”参数传递给 BeautifulSoup 构造函数:

for link in BeautifulSoup(response, "html.parser", parse_only=SoupStrainer('a')):


查看完整回答
反对 回复 2021-11-09
  • 1 回答
  • 0 关注
  • 393 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信