python - 如何将 '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?
解决方案
您应该在此处添加它:
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)
推荐阅读
- azure - 我们可以将发布到 Azure Web App 的 .net 核心 API 导入 APIM
- javascript - 将 JavaScript 数组转换为 JSON 格式:JavaScript
- react-native - aws-amplify AWSIoTProvider 套接字错误:未定义
- list - Flutter - DrowpdownMunu 项目未从列表中填充
- r - 如何计算r中数据帧中每一行的非零元素的平均值
- android - Android TV:焦点移至 ScrollView 中的视图,该视图位于 ScrollView 的可见部分之外
- neural-network - 了解卷积层中的特征图 (PyTorch)
- python - Pymodm:ImportError(两个模型相互引用)
- sql - 如何在 Oracle SQL 中使用 row_number()
- laravel - Laravel Nova 获取 Action 中选定条目的数量