【发布时间】:2017-04-27 10:53:32
【问题描述】:
我正在使用漂亮的汤并请求从网页中记录信息,我正在尝试获取只是标题且不包括标题字体中的文本 title= 的书名列表。
示例文本='一堆垃圾标题=book1 更多垃圾文本标题=book2'
我得到的是 titleList = ['title=book1', 'title=book2']
我想要titleList = ['book1', 'book2']
我尝试过匹配组,这确实将 title= 和 book1 分开,但我不确定如何仅将 group(2) 附加到列表中。
titleList = []
def getTitle(productUrl):
res = requests.get(productUrl, headers=headers)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'lxml')
title = re.compile(r'title=[A-Za-z0-9]+')
findTitle = title.findall(res.text.strip())
titleList.append(findTitle)
【问题讨论】:
-
你能发布一个你正在使用的 html 的例子吗?
-
这真的是一个 BeautifulSoup 问题吗?你实际上并没有使用
soup对象。 -
问题是你为什么用beautifulsoup?
标签: python regex python-2.7 beautifulsoup