i have searched for similar questions but didn't find what i need.
I am searching a web for two attributes in this case red
and green
in span
from urllib.request import urlopen
from bs4 import BeautifulSoup
html=urlopen('http://www.pythonscraping.com/pages/warandpeace.html')
soup=BeautifulSoup(html,'html.parser')
nameList=soup.findAll("span",{"class":"red","class":"green"})
print(nameList)
However i am only getting the green attribute,i tried using
nameList,nameList2=soup.findAll("span",{"class":"red","class":"green"})
but i get the error ValueError: too many values to unpack (expected 2)
is there a way to print both and store each attribute in a namelist (without using multiple findAll
)
You can try to use CSS selector to match span
with both class names as below:
nameList = soup.select("span.red, span.green")
If you still want to use findAll
, try
nameList = soup.findAll("span",{"class":["red", "green"]})