本文整理汇总了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 + " "