0
这是我的源代码,我真的试图获得所有绿色和红色跨度,但实际上我只得到绿色的一个!BeautifulSoup查找所有功能多属性选择不正确的工作
有什么想法吗?
Python代码:
#!/usr/bin/python3
from urllib.request import urlopen
from urllib.error import HTTPError,URLError
from bs4 import BeautifulSoup
import re
import sys
def main():
src = urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
txt = src.read()
bsObj = BeautifulSoup(txt)
spans = bsObj.findAll('span',{'class':'red','class':'green'},recursive=True)
outfile = '''
<html>
<head></head>
<body>
'''
for span in spans:
outfile = outfile + str(span)
outfile = outfile + '</body></html>'
print(outfile)
main()
感谢所有,舍比
字典需要独特的键。 '{'class':'red','class':'green'}'实际上等同于'{'class':'green'}',因为'class'被覆盖。 – Shadow