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

我有一些返回错误的代码:UserWarning:没有明确指定解析器,所以我使用了这个系统最好的可用 HTML 解析器(“html.parser”)。这通常不是问题,但如果您在另一个系统上或在不同的虚拟环境中运行此代码,它可能会使用不同的解析器并表现出不同的行为。


导致此警告的代码位于文件 scrape7.py 的第 14 行。要消除此警告,请将附加参数 'features="html.parser"' 传递给 BeautifulSoup 构造函数。


我的代码是:


import numbers

import httplib2

import requests

import re

from bs4 import BeautifulSoup, SoupStrainer


http = 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)


神不在的星期二
浏览 402回答 1
1回答

喵喵时光机

将“html.parser”参数传递给 BeautifulSoup 构造函数:for link in BeautifulSoup(response, "html.parser", parse_only=SoupStrainer('a')):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python