我有像这样的HTML:
<a class="archive-title" target="_blank" href="http://python.jobbole.com/84100/" title="SQLAlchemy 和其他的 ORM 框架">SQLAlchemy 和其他的 ORM 框架</a><br />
2016/01/12 ·我想获取href、title和2016/01/11的内容。
我试着这样做:
soup = BeautifulSoup(contents)
result = soup.find_all("a", attrs={"class": "archive-title"})
for i in result:
print i.get('href') + " " + i.get('title')结果是:
http://python.jobbole.com/84100/ SQLAlchemy 和其他的 ORM 框架我不知道如何同时获得2016/01/11的日期。我该怎么办?
发布于 2016-01-13 22:39:33
如果它在整个页面中是相同的模式:
[a.parents.contents[-1] for a in soup.find_all('a')](如果父节点内容较多,请将索引改为-1以外的值)
发布于 2016-01-13 22:55:55
Regex始终是一个选项:
import re
src = """<a class="archive-title" target="_blank" href="http://python.jobbole.com/84100/" title="SQLAlchemy 和其他的 ORM 框架">SQLAlchemy 和其他的 ORM 框架</a><br />
2016/01/12 ·"""
print re.findall('(\d{4}/\d{2}/\d{2})', src.replace('\n','') )结果是:'2016/01/12‘
https://stackoverflow.com/questions/34768613
复制相似问题