使用yield优雅抓取网页分页数据
在使用Python来抓取网页数据的时候,常常碰到分页数据,其中部分下一页按钮已经有具体的链接地址,而另外的可能是javascript来处理分页的。这要求同时能解析页面内容,又要采集下一页的url。怎样优雅用python写这样的代码呢?或者说怎样更pythonic?
下面分别给出部分代码实例
def get_next_page(obj): '''get next page content from a url or another content ''' error_occurred = False for retry2 in xrange(3): try: if isinstance(obj, (basestring, unicode)): resp = curr_session.get(obj, timeout=TIMEOUT, headers=headers, cookies=cookies, allow_redirects=True) content = resp.content save_html_content(obj, content) error_occurred = False else: content = obj soup = BeautifulSoup(content, features='html5lib', from_encoding="utf8") e_next_page = soup.find('a', text="下頁") break except: error_occurred = True time.sleep(2) if error_occurred: yield content return if e_next_page: next_url = "http://www.etnet.com.hk" + e_next_page.get('href') time.sleep(2) yield content for i in get_next_page(next_url): yield i else: yield content
def get_next_page(obj, page=1): '''get next page content from a url or another content ''' error_occurred = False for retry2 in xrange(3): try: if isinstance(obj, (basestring, unicode)): resp = curr_session.get(obj, timeout=TIMEOUT, headers=headers, cookies=cookies, allow_redirects=True) content = resp.content save_html_content(obj, content) hrefs = re.findall('industrysymbol=.*&market_id=[^;]+', content) if page == 1 and (not "sh=" in obj) and hrefs: reset_url = ("http://www.aastocks.com/tc/cnhk/market/industry" "/sector-industry-details.aspx?%s&page=1" % \ (hrefs[0].replace('sh=1', 'sh=0').replace('&page=', '') \ .replace("'", '').split()[0])) for next_page in get_next_page(reset_url): yield next_page return error_occurred = False else: content = obj soup = BeautifulSoup(content, features='html5lib', from_encoding="utf8") e_next_page = soup.find('td', text="下一頁 ") break except: error_occurred = True LOG.error(traceback.format_exc()) time.sleep(2) if error_occurred: yield content return if e_next_page: hrefs = re.findall('industrysymbol=.*&market_id=[^;]+', content) if hrefs: next_url = ("http://www.aastocks.com/tc/cnhk/market/industry/sector-industry" "-details.aspx?%s&page=%d" % \ (hrefs[0].replace('sh=1', 'sh=0') \ .replace('&page=', '').replace("'", '').split()[0], page+1)) time.sleep(2) yield content for next_page in get_next_page(next_url, page+1): yield next_page else: yield content
for curr_href in e_href: retry_interval = random.randint(MIN_INTERVAL_SECONDS_FOR_RETRIEVING, MAX_INTERVAL_SECONDS_FOR_RETRIEVING) time.sleep(retry_interval) contents = get_next_page(curr_href) for content in contents: get_page_data(content)
相关推荐
python库,解压后可用。 资源全名:ffmpeg_progress_yield-0.1.2-py2.py3-none-any.whl
初学 Python 的开发者经常会发现很多 Python 函数中用到了 yield 关键字,然而,带有 yield 的函数执行流程却和普通函数不一样,yield 到底用来做什么,为什么要设计 yield ?本文将由浅入深地讲解 yield 的概念和...
python中yield的用法全文共3页,当前为第1页。python中yield的用法全文共3页,当前为第1页。python中yield的用法 python中yield的用法全文共3页,当前为第1页。 python中yield的用法全文共3页,当前为第1页。 Python...
python方法 python_yield用法详解
主要介绍了python 如何区分return和yield,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
主要介绍了Python中的yield函数,yield函数是生成器中的一个常用函数,本文来自于IBM官方网站的开发者文档的翻译,需要的朋友可以参考下
资源分类:Python库 所属语言:Python 资源全名:pytest-yield-1.0.0.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
yield from 是 Python3.3 后新加的语言结构。yield from的主要功能是打开双向通道,把最外层的调用方法与最内层的子生成器连接起来。这两者就可以进行发送值和返回值了,yeild from结构的本质是简化嵌套的生产器,不...
但是有一些话题,大多数学生只有很少,或者完全没有任何接触,尤其是“生成器和yield关键字”。我猜这对大多数新手Python程序员也是如此。 有事实表明,在我花了大功夫后,有些人仍然不能理解生成器和yield关键字。...
今天在看其他同事的代码时,发现一个没使用过的python关键字 :yield 先问了一下同事,听他说了几句,有个模糊的印象,仅仅是模糊而已。于是自己去搜搜资料看。看了半天,逐渐清晰了。不过在工作机制以及应用上...
开始使用Python Python中的数据类型 Python中的变量 Python中的控制语句 Python中的函数 Python中的类 Python对象的序列化 Python中的正则表达式 Python多线程编程 Python中的socket编程 Python设计模式...
python中生成器是迭代器的一种,使用yield返回函数值。每次调用yield会暂停,而可以使用next()函数和send()函数可以恢复生成器。 这里可以参考Python函数式编程指南:对生成器全面讲解 注意到yield是个表达式而不仅仅...
主要介绍了对python中return与yield的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
主要介绍了python3.6生成器yield用法,结合实例形式分析了Python3.6中生成器yield的功能、用法及相关操作注意事项,需要的朋友可以参考下
Python中yield返回生成器的详细方法
方法如此多样的原因在于,数据“抓取”实际上包括很多问题:你不需要使用相同的工具从成千上万的页面中抓取数据,同时使一些Web工作流自动化(例如填一些表单然后取回数据)。我喜欢DIY的原因在于其灵活性,但是却不...
1. iterator叠代器最简单例子应该是数组下标了,且看下面的c++代码: ...上面的代码翻译成python: 复制代码 代码如下: array = [i for i in range(10)]for i in array: print i, 首先,array
在python 3.3里,generator新增了一个语法 yield from,这个yield from的作用是什么?语法是什么呢?下面通过这篇文章主要给大家详细介绍了Python 3中yield from语法的相关资料,需要的朋友可以参考借鉴,下面来一起...