Python ';str';对象没有属性';查找所有';靓汤
这是我的代码,非常简单。由于某些原因,获取上述错误。即使我删除了Python ';str';对象没有属性';查找所有';靓汤,python,beautifulsoup,Python,Beautifulsoup,这是我的代码,非常简单。由于某些原因,获取上述错误。即使我删除了text=str(html)并将soup=beautifulsop(text,'html.parser')替换为soup=beautifulsop(html,'html.parser'),也会出现同样的错误。发生什么事了 with urllib.request.urlopen('https://jalopnik.com/search?q=mazda&u=&zo=-07:00') as response: htm
text=str(html)
并将soup=beautifulsop(text,'html.parser')
替换为soup=beautifulsop(html,'html.parser')
,也会出现同样的错误。发生什么事了
with urllib.request.urlopen('https://jalopnik.com/search?q=mazda&u=&zo=-07:00') as response:
html = response.read()
text = str(html)
soup = BeautifulSoup(text, 'html.parser')
print(type(soup))
soup = soup.prettify()
print(soup.find_all('div'))
soup=soup.prettify()
返回一个字符串,因为您将其分配回了soup
,因此在调用soup.find_all()
时,soup
将成为一个字符串
从:
prettify()
方法将把一个漂亮的汤解析树转换成一个格式良好的Unicode字符串
不要用修饰过的细绳代替你的汤BeautifulSoup
不需要修饰,只有当您想将汤转换回字符串以保存到文件或调试时才需要修饰
soup = BeautifulSoup(text, 'html.parser')
print(soup.find_all('div'))
很好用
您也不希望使用str(html)
来解码字节
对象。通常你会使用html.decode('utf8')
或类似的代码str(html)
提供一个以b'
开头,以结尾的值。
然而,BeautifulSoup本身完全能够解码字节值。它还可以直接从响应中读取:
with urllib.request.urlopen('https://jalopnik.com/search?q=mazda&u=&zo=-07:00') as response:
soup = BeautifulSoup(response, 'html.parser')
print(soup.find_all('div'))
soup=soup.prettify()
使soup
成为一个字符串。不要用修饰过的细绳代替你的汤。