나는 Scrapy에서 기본적으로 벌금을 내고 정확하게 수행하고있는 거미를 작성했다. 문제는 작은 변화가 필요하며 성공없이 여러 방법을 시도했습니다 (예 : InitSpider 수정). 여기에 스크립트가 지금 어떻게해야되는 것입니다 : 지금 규칙에 정의 된 패턴과 여기에서 크롤링을 시작 치료에서 CrawlSpider 초기화하기
http://www.example.de/index/search?filter=homepage
- 크롤 시작 URL
http://www.example.de/index/search?method=simple
- 을
기본적으로 변경해야하는 것은 그 사이에 하나의 URL을 호출하는 것입니다. BaseSpider로 모든 것을 다시 쓰지 않으려 고합니다. 그래서 누군가가 이것을 성취하는 방법에 대한 아이디어를 가지고 있기를 바랍니다. :)
추가 정보가 필요하면 알려주십시오. 아래에서 현재 스크립트를 찾을 수 있습니다.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
from example.items import ExampleItem
from scrapy.contrib.loader.processor import TakeFirst
import re
import urllib
take_first = TakeFirst()
class ExampleSpider(CrawlSpider):
name = "example"
allowed_domains = ["example.de"]
start_url = "http://www.example.de/index/search?method=simple"
start_urls = [start_url]
rules = (
# http://www.example.de/index/search?page=2
# http://www.example.de/index/search?page=1&tab=direct
Rule(SgmlLinkExtractor(allow=('\/index\/search\?page=\d*$',)), callback='parse_item', follow=True),
Rule(SgmlLinkExtractor(allow=('\/index\/search\?page=\d*&tab=direct',)), callback='parse_item', follow=True),
)
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
# fetch all company entries
companies = hxs.select("//ul[contains(@class, 'directresults')]/li[contains(@id, 'entry')]")
items = []
for company in companies:
item = ExampleItem()
item['name'] = take_first(company.select(".//span[@class='fn']/text()").extract())
item['address'] = company.select(".//p[@class='data track']/text()").extract()
item['website'] = take_first(company.select(".//p[@class='customurl track']/a/@href").extract())
# we try to fetch the number directly from the page (only works for premium entries)
item['telephone'] = take_first(company.select(".//p[@class='numericdata track']/a/text()").extract())
if not item['telephone']:
# if we cannot fetch the number it has been encoded on the client and hidden in the rel=""
item['telephone'] = take_first(company.select(".//p[@class='numericdata track']/a/@rel").extract())
items.append(item)
return items
편집 여기
InitSpider 내 시도 : https://gist.github.com/150b30eaa97e0518673a 여기에서 그런 생각 가지고 : Crawling with an authenticated session in Scrapy
당신은 여전히 CrawlSpider에서 상속 볼 수 있듯이,하지만 내가 만든이 핵심 Scrapy 파일에 대한 일부 변경 사항 (필자가 선호하는 방식이 아님). CrawlSpider는 BaseSpider (source) 대신 InitSpider를 상속받습니다.
이것은 지금까지는 작동하지만 스파이더는 다른 페이지를 모두 차지하는 대신 첫 번째 페이지에서 멈 춥니 다.
또한,이 방법은 자신을
이