Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';str';对象没有属性';查找所有';靓汤_Python_Beautifulsoup - Fatal编程技术网

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
成为一个字符串。不要用修饰过的细绳代替你的汤。