当前位置: 首页>>代码示例>>Python>>正文


Python URL.by_tag方法代码示例

本文整理汇总了Python中pattern.web.URL.by_tag方法的典型用法代码示例。如果您正苦于以下问题:Python URL.by_tag方法的具体用法?Python URL.by_tag怎么用?Python URL.by_tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pattern.web.URL的用法示例。


在下文中一共展示了URL.by_tag方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: URL

# 需要导入模块: from pattern.web import URL [as 别名]
# 或者: from pattern.web.URL import by_tag [as 别名]
 # I found that there is a page with 10 reviews about this book:
 # http://www.amazon.fr/product-reviews/2266219154/
 # So we want to parse the book id from the first link and mine its reviews page:
 id = a.split("/")[-2]
 reviews = "http://www.amazon.fr/product-reviews/" + id + "/"
 print reviews
 
 # We can use Chrome's Developer Tools to inspect the HTML of the review page.
 # It turns out the reviews are contained in a <table id="productReviews"> element.
 # This table has one row and two columns.
 # Each <div> in the first column is a review.
 # If the table is absent, it means there are no reviews for this book.
 reviews = URL(reviews).download(cached=True, throttle=20) # throttle = delay between crawls
 reviews = DOM(reviews).by_id("productReviews")
 if reviews is not None:
     for review in reviews.by_tag("div"):
         # We use a try-except statement to brute-force it:
         # The <div>'s in the table do not have a class to search for,
         # and there may be other <div>'s in-between, which end up in the except-block. 
         try:
             # The star rating is <span class="swSprite s_star_5_0 " title="5.0 etoiles sur 5">.
             score = review.by_class("swSprite")[0]
             score = score.attributes["title"]
             score = score.split(" ")[0]
             score = float(score)
         
             # The review is contained as plain text in the <div>.
             text = ""
             for child in review.children:
                 if child.type == "text":
                     text += child.source + " "
开发者ID:HappySustainableLife,项目名称:tomdesmedt,代码行数:33,代码来源:2-amazon.py


注:本文中的pattern.web.URL.by_tag方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。