我试图从内容中获取文本,但是当我尝试使用结果变量上的漂亮汤函数时,它会导致错误。
from bs4 import BeautifulSoup as bs
import requests
webpage = 'http://www.dictionary.com/browse/coypu'
r = requests.get(webpage)
page_text = r.text
soup = bs(page_text, 'html.parser')
result = soup.find_all('meta', attrs={'name':'description'})
print (result.get['contents'])我正试着把结果读出来;
Coypu定义,一种大型南美水生啮齿动物,Myocastor (或Myopotamus),产毛皮营养。详情见。
发布于 2016-08-20 13:36:14
soup.find_all()返回一个列表。因为在您的示例中,它只返回列表中的一个元素,所以可以这样做:
>>> type(result)
<class 'bs4.element.ResultSet'>
>>> type(result[0])
<class 'bs4.element.ResultSet'>
>>> result[0].get('content')
Coypu definition, a large, South American, aquatic rodent, Myocastor (or Myopotamus) coypus, yielding the fur nutria. See more.发布于 2016-08-20 14:00:19
当您只希望第一个或单个标记使用find时,find_all返回一个list/resultSet
result = soup.find('meta', attrs={'name':'description'})["contents"]您还可以在select_one中使用css选择器。
result = soup.select_one('meta[name=description]')["contents"]发布于 2016-08-21 12:50:39
您不需要使用findall,因为只有使用find才能获得所需的输出‘
from bs4 import BeautifulSoup as bs
import requests
webpage = 'http://www.dictionary.com/browse/coypu'
r = requests.get(webpage)
page_text = r.text
soup = bs(page_text, 'html.parser')
result = soup.find('meta', {'name':'description'})
print result.get('content')它将印刷:
Coypu definition, a large, South American, aquatic rodent, Myocastor (or Myopotamus) coypus, yielding the fur nutria. See more.https://stackoverflow.com/questions/39054666
复制相似问题