【发布时间】:2016-08-16 05:42:04
【问题描述】:
哪里出错了?我想解析没有标签的文本。
from bs4 import BeautifulSoup
import re
import urllib.request
f = urllib.request.urlopen("http://www.championat.com/football/news-2442480-orlov-zenit-obespokoen---pole-na-novom-stadione-mozhet-byt-nekachestvennym.html")
soup = BeautifulSoup(f, 'html.parser')
soup=soup.find_all('div', class_="text-decor article__contain")
invalid_tags = ['b', 'i', 'u', 'br', 'a']
for tag in invalid_tags:
for match in soup.find_all(tag):
match.replaceWithChildren()
soup = ''.join(map(str, soup.contents))
print (soup)
错误:
Traceback (most recent call last):
File "1.py", line 9, in <module>
for match in soup.find_all(tag):
AttributeError: 'ResultSet' object has no attribute 'find_all'
【问题讨论】:
-
您将
soup替换为结果集:soup=soup.find_all('div', class_="text-decor article__contain")。结果集只是一个列表,其中包含对原始汤对象的额外引用。我不清楚为什么要用结果集替换BeautifulSoup对象,如果您想进行嵌套搜索,请改用 CSS selector。 -
你真的很想看output formatting,不要将对象映射到字符串。
标签: python beautifulsoup resultset findall