【发布时间】:2014-04-18 14:49:04
【问题描述】:
我正在编写一个 python 脚本来进行网络抓取。我想在网页上找到给定部分的基本 URL,如下所示:
<div class='pagination'>
<a href='webpage-category/page/1'>1</a>
<a href='webpage-category/page/2'>2</a>
...
</div>
所以,我只需要从第一个 href 中获取除 number('webpage-category/page/') 之外的所有内容,并且我有以下工作代码:
pages = [l['href'] for link in soup.find_all('div', class_='pagination')
for l in link.find_all('a') if not re.search('pageSub', l['href'])]
s = pages[0]
f = ''.join([i for i in s if not i.isdigit()])
问题是,生成这个列表是一种浪费,因为我只需要第一个 href。我认为发电机会是答案,但我无法做到这一点。也许你们可以帮助我使这段代码更简洁?
【问题讨论】:
标签: python html web-scraping html-parsing beautifulsoup