首页 > 解决方案 > 如何将 'features="html.parser"' 添加到 BeautifulSoup 构造函数

问题描述

response = requests.get(url)
bs = BeautifulSoup(response.text)
rows = bs.find('table', {'class': "infobox"}).find_all('tr')
list1 = []
for i,tr in enumerate(rows):
  cells = tr.find_all('td')
  if len(cells) == 2:
    list1.append(cells[0].text.strip(":"))
    list1.append(cells[1].text.strip('\n'))
res_dct = {list1[i]: list1[i + 1] for i in range(0, len(list1), 2)}
print (res_dct)

在该行中,rows = bs.find('table', {'class': "infobox"}).find_all('tr')我可以在哪里添加 features="html.parser?

标签: pythonbeautifulsoupcode-formatting

解决方案


您应该在此处添加它:

bs = BeautifulSoup(response.text, "html.parser")

所以它看起来像这样(基于您的代码):

import requests
from bs4 import BeautifulSoup


response = requests.get(url)
bs = BeautifulSoup(response.text, "html.parser")
rows = bs.find('table', {'class': "infobox"}).find_all('tr')
list1 = []
for i, tr in enumerate(rows):
    cells = tr.find_all('td')
    if len(cells) == 2:
        list1.append(cells[0].text.strip(":"))
        list1.append(cells[1].text.strip('\n'))
res_dct = {list1[i]: list1[i + 1] for i in range(0, len(list1), 2)}
print(res_dct)

推荐阅读