我正在使用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
的思考?
我對BeautifulSoup沒有任何經驗。它是否在Scrapy內部運行?我已經在上面添加了新的代碼,你會不會建議BeautifulSoup?謝謝。 @PepperoniPizza – SMPLGRP
@benknighthorse看看這個新的例子,在你的計算機上試試它,看看結果。 – PepperoniPizza
這很棒@PepperoniPizza。我跑了腳本,它按預期工作。現在我需要將其添加到Scrapy中併爲它提供這些結果。我不知道如何/從哪裏開始。你能給我一個指針或地方開始? – SMPLGRP