خرید بک لینک

Vote count: 0

I am trying to write some code to scrap the website of a UK housebuilder to record a list of houses for sale.

I am starting on the page http://www.persimmonhomes.com/sitemap and I have written one part of the code to list all the urls of the housebuilder developments and then the second part of the code to scrap from each of the urls to record prices etc.

I know the second part works and I know that the first part lists out all the urls. But for some reason the urls listed by the first part don't seem want to trigger the second part of the code to scrap from them.

The code of this first part is:

def parse(self, response):
    for href in response.xpath('//*[@class="contacts-item"]/ul/li/a/@href'):
       url = urlparse.urljoin('http://www.persimmonhomes.com/',href.extract())
       yield scrapy.Request(url, callback=self.parse_dir_contents)

Now, I know this lists the urls I want (if I put in the line "print url" then they all get listed) and I can manually list add them to the code to run the second part all ok if I wanted to. However, even though the urls are created they do not seem to allow the second part of the code to scrap from them.

and the entire code is below:

import scrapy
import urlparse

from Persimmon.items import PersimmonItem

class persimmonSpider(scrapy.Spider):
    name = "persimmon"
    allowed_domains = ["http://www.persimmonhomes.com/"]
    start_urls = [
        "http://www.persimmonhomes.com/sitemap",        
    ]

def parse(self, response):
    for href in response.xpath('//*[@class="contacts-item"]/ul/li/a/@href'):
       url = urlparse.urljoin('http://www.persimmonhomes.com/',href.extract())
       yield scrapy.Request(url, callback=self.parse_dir_contents)

def parse_dir_contents(self, response):
    for sel in response.xpath('//*[@id="aspnetForm"]/div[4]'):
       item = PersimmonItem()
       item['name'] = sel.xpath('//*[@id="aspnetForm"]/div[4]/div[1]/div[1]/div/div[2]/span/text()').extract()
       item['address'] = sel.xpath('//*[@id="XplodePage_ctl12_dsDetailsSnippet_pDetailsContainer"]/div/*[@itemprop="postalCode"]/text()').extract()
       plotnames = sel.xpath('//div[@class="housetype js-filter-housetype"]/div[@class="housetype__col-2"]/div[@class="housetype__plots"]/div[not(contains(@data-status,"Sold"))]/div[@class="plot__name"]/a/text()').extract()
       plotnames = [plotname.strip() for plotname in plotnames]
       plotids = sel.xpath('//div[@class="housetype js-filter-housetype"]/div[@class="housetype__col-2"]/div[@class="housetype__plots"]/div[not(contains(@data-status,"Sold"))]/div[@class="plot__name"]/a/@href').extract()
       plotids = [plotid.strip() for plotid in plotids]
       plotprices = sel.xpath('//div[@class="housetype js-filter-housetype"]/div[@class="housetype__col-2"]/div[@class="housetype__plots"]/div[not(contains(@data-status,"Sold"))]/div[@class="plot__price"]/text()').extract()
       plotprices = [plotprice.strip() for plotprice in plotprices]
       result = zip(plotnames, plotids, plotprices)
       for plotname, plotid, plotprices in result:
           item['plotname'] = plotname
           item['plotid'] = plotid
           item['plotprice'] = plotprice
           yield item

any views as to why the first part of the code creates the urls but the second part does not loop through them?

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 20 تير 1395 ساعت: 8:55

صفحه بندی