【发布时间】:2015-08-14 22:45:28
【问题描述】:
我的 items.py 文件是这样的:
from scrapy.item import Item, Field
class SpiItem(Item):
title = Field()
lat = Field()
lng = Field()
add = Field()
蜘蛛是:
import scrapy
import re
from spi.items import SpiItem
class HdfcSpider(scrapy.Spider):
name = "hdfc"
allowed_domains = ["hdfc.com"]
start_urls = ["http://hdfc.com/branch-locator"]
def parse(self,response):
addresses = response.xpath('//script')
for sel in addresses:
item = SpiItem()
item['title'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="title":).+(?=")')
item['lat'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="latitude":).+(?=")')
item['lng'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="longitude":).+(?=")')
item['add'] = sel.xpath('//script[@type="text/javascript"][1]').re('(?<="html":).+(?=")')
yield item
整个javascript代码,查看页面源代码,写在里面://html/body/table/tbody/tr[348]/td[2]。
为什么我的代码不起作用? 我只想提取 items 文件中提到的四个字段。
【问题讨论】:
-
请修正你的缩进。
标签: javascript python regex web-scraping scrapy