【发布时间】:2015-02-07 18:34:31
【问题描述】:
只睡了 3 小时,醒了 20 多小时很累,请原谅我的错误。
我正在尝试实现多个 xpath 选择器,但似乎无法获得它,显然这段代码有缺陷代码,重复描述,它最终获取最后一项的描述并将其分配给所有项目、屏幕截图和代码:
显示我在视觉表示中看到的意思: this http://puu.sh/fBjA9/da85290fc2.png
代码(Scrapy Web Crawler Python): 蜘蛛
def parse(self, response):
item = DmozItem()
for sel in response.xpath("//td[@class='nblu tabcontent']"):
item['title'] = sel.xpath("a/big/text()").extract()
item['link'] = sel.xpath("a/@href").extract()
for sel in response.xpath("//td[contains(@class,'framed')]"):
item['description'] = sel.xpath("b/text()").extract()
yield item
管道
def process_item(self, item, spider):
self.cursor.execute("SELECT * FROM data WHERE title= %s", item['title'])
result = self.cursor.fetchall()
if result:
log.msg("Item already in database: %s" % item, level=log.DEBUG)
else:
self.cursor.execute(
"INSERT INTO data(title, url, description) VALUES (%s, %s, %s)",
(item['title'][0], item['link'][0], item['description'][0]))
self.connection.commit()
log.msg("Item stored : " % item, level=log.DEBUG)
return item
def handle_error(self, e):
log.err(e)
感谢您阅读并提供帮助。
【问题讨论】:
-
不看html的scrapy代码意义不大;有网址吗?
-
@HughBothwell 在这里,谢谢。 phpclasses.org/browse/class/130.html
-
@HughBothwell 要睡觉了,6 小时后就会起床。近24小时不睡觉
标签: python mysql for-loop xpath scrapy