问题描述
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)