vBulletin フォーラム サイトをスクレイピングする次の単純なコードに問題があります。
class ForumSpider(CrawlSpider):
...
rules = (
Rule(SgmlLinkExtractor(restrict_xpaths="//div[@class='threadlink condensed']"),
callback='parse_threads'),
)
def parse_threads(self, response):
thread = HtmlXPathSelector(response)
# get the list of posts
posts = thread.select("//div[@id='posts']//table[contains(@id,'post')]/*")
# plist = []
for p in posts:
table = ThreadItem()
table['thread_id'] = (p.select("//input[@name='searchthreadid']/@value").extract())[0].strip()
string_id = p.select("../@id").extract() # returns a list
p_id = string_id[0].split("post")
table['post_id'] = p_id[1]
# plist.append(table)
# return plist
yield table
いくつかの xpath ハックは別として、yieldでこれを実行すると、同じ thread_id と post_id に対して複数のヒットが発生し、非常に奇妙な結果が得られます。何かのようなもの:
114763,1314728
114763,1314728
114763,1314728
114763,1314740
114763,1314740
114763,1314740
return (コメント内) を使用して同じロジックに戻すと、すべて正常に動作します。ジェネレーターの基本的な間違いかもしれないと思いますが、わかりません。同じ投稿が何度もヒットするのはなぜですか? コードがreturnを使用して動作するのにyieldを使用しないのはなぜですか?
要旨の完全なコード スニペットはこちら.