我正在使用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