1

我正在使用scrapy,我想通过www.rentler.com 进行搜索。我已经去网站搜索了我感兴趣的城市,这里是那个搜索结果的链接:

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 并将其附加到 url。这样它就可以进入每一页,并获取我感兴趣的数据。

我想我需要这样的东西:

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),)

更新 *感谢您的输入。这是我现在拥有的,它似乎可以运行但不会刮擦: *

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

想法?

4

2 回答 2

7

如果您需要从 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']

这个小脚本将获取HTML 标记href内的所有内容。a

编辑:功能齐全的脚本:

我在我的电脑上测试了这个,结果和预期的一样,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']

那只向您展示如何从您尝试抓取的html页面中抓取href,当然您可以在scrapy中使用它,正如我告诉您的,BeautifulSoup只需要纯HTML,这就是我使用requests.get(url).text并且您可以抓取的原因那。所以我猜scrapy可以将纯HTML传递给BeautifulSoup。

编辑 2 好的,我认为你根本不需要 scrapy,所以如果前面的脚本为你提供了你想要从作品中获取数据的所有链接,你只需要做这样的事情:

假设我有一个有效的 url 列表,我想从中获取特定数据,比如价格、英亩、地址......你可以只使用前面的脚本而不是将 url 打印到屏幕上,你可以将它们附加到列表并仅附加以 .开头的那些/listing/。这样你就有了一个有效的 url 列表。

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

您只需要查看源代码,您就会了解如何从每个 url 中抓取您需要的数据。

于 2013-10-17T14:19:47.843 回答
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
于 2013-10-17T14:29:43.253 回答