我尝试使用以下代码从Web URL读取文本,将所有有效字符串存储到我稍后可以操作的变量中。我在运行时遇到错误
from bs4 import BeautifulSoup
import urllib.request
from django.template.defaultfilters import title
response = urllib.request.urlopen('http://www.scotland.org/about-scotland/facts-about-scotland/')
data = response.read()
soup = BeautifulSoup(data)
textString = soup.findAll('p').getText()
print(textString)
错误:
textString = soup.findAll('p').getText()
AttributeError: 'ResultSet' object has no attribute 'getText'
答案 0 :(得分:2)
试试这个:
textString = soup.findAll('p')[0].getText()
如果您想获取所有paragraph
数据,请尝试以下操作:
elements = soup.findAll('p')
for paragraph in elements:
print paragraph.getText()
答案 1 :(得分:0)
我找到了解决方法。我想你的编辑想法一样,谢谢!
textString = ""
for i in soup.find_all('p'):
textString += i.getText()
print(textString)