-1
import scrapy
class Pttscrapper2Spider(scrapy.Spider):
name = 'PTTscrapper2'
allowed_domains = ['https://www.ptt.cc']
start_urls = ['https://www.ptt.cc/bbs/HatePolitics/index.html/']
handle_httpstatus_list = [400, 302]
def parse(self, response):
urls = response.css('div.r-ent > div.title > a::attr(href)').extract()
for thread_url in urls:
url = response.urljoin(thread_url)
yield scrapy.Request(url=url, callback=self.parse_details)
next_page_url = response.css('a.wide:nth-child(2)::attr(href)').extract_first()
if next_page_url:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(url=next_page_url, callback=self.parse)
def parse_details(self, response):
yield {
'title' : response.xpath('//head/title/text()').extract(),
'stance' : response.xpath('//*[@id="main-content"]/div[@class="push"]/span[1]/text()').extract(),
'userid' : response.xpath('//*[@id="main-content"]/div[@class="push"]/span[2]/text()').extract(),
'comment' : response.xpath('//*[@id="main-content"]/div[@class="push"]/span[3]/text()').extract(),
'time_of_post' : response.xpath('//*[@id="main-content"]/div[@class="push"]/span[4]/text()').extract(),
}
我一直在使用上面的蜘蛛,試圖抓取網站,但我當我運行的蜘蛛,我得到的消息:Scrapy爬(302)狀態,如何處理
> 2017-10-05 23:14:27 [scrapy.core.engine] INFO: Spider opened
> 2017-10-05 23:14:27 [scrapy.extensions.logstats] INFO: Crawled 0 pages
> (at 0 pages/min), scraped 0 items (at 0 items/min) 2017-10-05 23:14:27
> [scrapy.extensions.telnet] DEBUG: Telnet console listening on
> 127.0.0.1:6023 2017-10-05 23:14:28 [scrapy.downloadermiddlewares.cookies] DEBUG: Received cookies from:
> <302 https://www.ptt.cc/bbs/HatePolitics/index.html/> Set-Cookie:
> __cfduid=d3ca57dcab04acfaf256438a57c547e4a1507216462; expires=Fri, 05-Oct-18 15:14:22 GMT; path=/; domain=.ptt.cc; HttpOnly
>
> 2017-10-05 23:14:28 [scrapy.core.engine] DEBUG: Crawled (302) <GET
> https://www.ptt.cc/bbs/HatePolitics/index.html/> (referer: None)
> 2017-10-05 23:14:28 [scrapy.core.engine] INFO: Closing spider
> (finished)
我一直在想的是我的蜘蛛似乎無法訪問索引中的子論壇。我測試了選擇器指向正確的位置,request.urljoin創建了正確的絕對url,但似乎無法訪問頁面中的子論壇。如果有人能告訴我爲什麼蜘蛛無法訪問鏈接,這將是非常棒的!
工作
日誌爲什麼你把'handle_httpstatus_list'?你沒有處理這些錯誤代碼,你應該刪除它 –
我已經刪除了handle_httpstatus,但現在的錯誤是,我被導向到錯誤的網址? 'code' 2017-10-06 00:46:33 [scrapy.downloadermiddlewares.redirect] DEBUG:重定向(302)到 from 2017-10-06 00:46:36 [scrapy.core.engine] DEBUG:Crawled(404)< GET https://www.ptt.cc/bbs/HatePolitics/index.html/index.html>(referer:None) 'code' –
BladeEntity