现在我用美汤来解析html页面。但有时我通过find_all得到的结果比页面数还少。例如,此页面http://www.totallyfreestuff.com/index.asp?m=0&sb=1&p=5有18个标题跨度。但是当我使用下面的代码时,它只得到了两个!谁能告诉我为什么。提前谢谢你!
soup = BeautifulSoup(page, 'html.parser')
hrefDivList = soup.find_all("span", class_ = "headline")
#print hrefDivList
print len(hrefDivList)发布于 2015-02-11 17:46:35
你可以尝试使用不同的解析器来解析Beautifulsoup。
import requests
from bs4 import BeautifulSoup
url = "<your url>"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
hrefDivList = soup.find_all("span", attrs={"class": "headline"})
print len(hrefDivList)发布于 2015-02-11 17:51:21
你可以尝试CSS选择器,让你的生活更轻松。
hrefDivList = soup.select("span.headline")
#print hrefDivList
print len(hrefDivList)或者,您可以直接迭代每个Span文本
for every_span in soup.select("span.headline"):
print(every_span.text)https://stackoverflow.com/questions/28447522
复制相似问题