【问题标题】:How to add 'features="html.parser"' to the BeautifulSoup constructor如何将 'features="html.parser"' 添加到 BeautifulSoup 构造函数
【发布时间】:2021-01-24 22:48:56
【问题描述】:
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?

【问题讨论】:

  • 创建soup对象时添加bs = BeautifulSoup(response.text, 'html.parser' )

标签: python beautifulsoup code-formatting


【解决方案1】:

你应该在这里添加它:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2021-06-15
    • 2020-07-01
    • 1970-01-01
    • 2014-03-20
    • 2011-08-19
    相关资源
    最近更新 更多