2013-10-17 124 views
1

我正在使用scrapy,我想通过www.rentler.com刮。我已经到网站和搜索的结果我很感兴趣的城市,这里是搜索结果的链接:通过网站与href参考通过

https://www.rentler.com/search?Location=millcreek&MaxPrice= 

现在,所有我感兴趣的是包含在页面上的房源,并且我想递归地一一浏览它们。

每件物品下会列出:

<body>/<div id="wrap">/<div class="container search-res">/<ul class="search-results"><li class="result"> 

每个结果都有一个<a class="search-result-link" href="/listing/288910">

我知道,我需要创建为crawlspider的规则,并把它看的是href和追加到网址。 。这样,它可以去的每一页,并抓住这些数据,我很感兴趣,

我想我需要这样的:

rules = (Rule(SgmlLinkExtractor(allow="not sure what to insert here, but this is where I think I need to href appending", callback='parse_item', follow=true),) 

UPDATE * 谢谢你的输入。以下是我现在,它似乎运行,但不刮: *

import re 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from KSL.items import KSLitem 

class KSL(CrawlSpider): 
    name = "ksl" 
    allowed_domains = ["https://www.rentler.com"] 
    start_urls = ["https://www.rentler.com/ksl/listing/index/?sid=17403849&nid=651&ad=452978"] 
    regex_pattern = '<a href="listing/(.*?) class="search-result-link">' 

    def parse_item(self, response): 
     items = [] 
     hxs = HtmlXPathSelector(response) 
     sites = re.findall(regex_pattern, "https://www.rentler.com/search?location=millcreek&MaxPrice=") 

     for site in sites: 
      item = KSLitem() 
      item['price'] = site.select('//div[@class="price"]/text()').extract() 
      item['address'] = site.select('//div[@class="address"]/text()').extract() 
      item['stats'] = site.select('//ul[@class="basic-stats"]/li/div[@class="count"]/text()').extract() 
      item['description'] = site.select('//div[@class="description"]/div/p/text()').extract() 
      items.append(item) 
     return items 

的思考?

回答

3

如果需要抽取数据出一个html文件,其中是这样,我会建议使用BeautifulSoup,它的安装和使用非常简单:

from bs4 import BeautifulSoup 

bs = BeautifulSoup(html) 
for link in bs.find_all('a'): 
    if link.has_attr('href'): 
     print link.attrs['href'] 

这个小脚本将得到所有href那在a HTML标签内。

编辑:全功能的脚本:

我测试了我的电脑上,并如预期的结果,BeautifulSoup需要纯HTML,你可以刮,你需要在它外面是什么,看看这个代码:

import requests 
from bs4 import BeautifulSoup 

html = requests.get(
    'https://www.rentler.com/search?Location=millcreek&MaxPrice=').text 
bs = BeautifulSoup(html) 
possible_links = bs.find_all('a') 
for link in possible_links: 
    if link.has_attr('href'): 
     print link.attrs['href'] 

那只能说明你如何HREF刮出你想刮HTML页面,当然你也可以使用它里面scrapy,因为我告诉你,BeautifulSoup只需要普通的HTML,这就是为什么我使用requests.get(url).text,你可以刮掉。所以我想scrapy可以将简单的HTML传递给BeautifulSoup。

编辑2 好吧,看我不认为你需要scrapy可言,所以如果前面的脚本让你所有你想从作品取数据的链接,你只需要做这样的事情:

假设我有一个有效的url列表我想从例如price,acres,address得到具体的数据......你可以只用这个脚本而不是打印urls来屏蔽你可以附加到一个列表并只追加以/listing/开头的列表。这样你就有了一个有效的url列表。

for url in valid_urls: 
    bs = BeautifulSoup(requests.get(url).text) 
    price = bs.find('span', {'class': 'amount'}).text 
    print price 

你只需要看看源代码,你会得到怎样刮你的每一个网址所需要的数据的想法。

+0

我对BeautifulSoup没有任何经验。它是否在Scrapy内部运行?我已经在上面添加了新的代码,你会不会建议BeautifulSoup?谢谢。 @PepperoniPizza – SMPLGRP

+0

@benknighthorse看看这个新的例子,在你的计算机上试试它,看看结果。 – PepperoniPizza

+0

这很棒@PepperoniPizza。我跑了脚本,它按预期工作。现在我需要将其添加到Scrapy中并为它提供这些结果。我不知道如何/从哪里开始。你能给我一个指针或地方开始? – SMPLGRP

0

您可以使用正则表达式从链接中查找所有出租家庭ID。从那里,您可以使用您拥有的ID并取而代之。

import re 
regex_pattern = '<a href="/listing/(.*?)" class="search-result-link">' 
rental_home_ids = re.findall(regex_pattern, SOURCE_OF_THE_RENTLER_PAGE) 
for rental_id in rental_home_ids: 
    #Process the data from the page here. 
    print rental_id 

编辑: 这里的工作,在其通自己版本的代码。它打印所有链接ID。您可以按原样使用它。

import re 
import urllib 
url_to_scrape = "https://www.rentler.com/search?Location=millcreek&MaxPrice=" 
page_source = urllib.urlopen(url_to_scrape).read() 
regex_pattern = '<a href="/listing/(.*?)" class="search-result-link">' 
rental_home_ids = re.findall(regex_pattern, page_source) 
for rental_id in rental_home_ids: 
    #Process the data from the page here. 
    print rental_id 
+0

感谢您的建议。我已经添加了代码,它运行时没有错误,但不是在拼凑。你可以看一下吗? @GKBRK – SMPLGRP

+0

我想我找到了错误@benknighthorse。你把链接放在re.findall()中。相反,您需要放置页面源代码。我不知道它如何完成scrapy,但可能并不困难。 – GKBRK

+0

感谢您的快速回复@GKBRK。什么是SOURCE_OF_THE_RENTLER_PAGE? – SMPLGRP