scrapy爬虫爬取旅游问答网站,递归爬取下一页

本文介绍了一款针对新浪旅游问答板块的爬虫程序设计,该程序能够抓取多个分类下的问题及其答案,并将结果存储到本地文件中。爬虫通过分析网页结构,使用XPath表达式来定位和提取所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class BusSpider(BaseSpider):
    #设置爬虫名称
    name = "xinlang"
    #设置起始URL列表
    start_urls = ["http://travel.sina.com.cn/list-wenda/all/5"]

    def parse(self, response):
        req = []
        hxs = HtmlXPathSelector(response)
        how=hxs.x('/html/body/div[5]/div[1]/div[2]/div[1]/a[5]/text()').extract()
        #print 'how many--',how[0].strip()
        if int(how[0].strip())!= int(5):
            #print 'not ==',how[0].strip()
            nn=hxs.x('/html/body/div[5]/div[1]/div[2]/div[1]/a[9]/@href').extract()
            #print '------',len(nn)
            n=Request(url="http://travel.sina.com.cn" + nn[0].strip(),callback=self.parse)
            yield n
        cat_urls =  hxs.x('/html/body/div[5]/div[1]/div[1]/ul/li/a/@href').extract()
        print 'cat_urls =', cat_urls
        for url in cat_urls:
            #构建新的URL
            new_url = "http://travel.sina.com.cn" + url
            print "[parse]new_url = %s" % (new_url)
            #创建对应的页面的Request对象,设定回调函数为parse_cat,利用parse_cat处理返回的页面
            r = Request(new_url, callback=self.parse_cat)
            yield r
        return


    def parse_cat(self, response):
        hxs = HtmlXPathSelector(response)
        title = hxs.x('//h1[@id="artibodyTitle"]/text()').extract()
        des= hxs.x('/html/body/div[5]/div[1]/div[1]/p/text()').extract()
        ans=hxs.x('/html/body/div[5]/div[1]/div[2]/ul/li/p/text()').extract()
#结果写入到记录的文件之中
        print "#####"
        global SUM
        SUM+=1
        fp = codecs.open('record'+str(SUM)+'.txt', 'w', 'utf-8') 
        if filter_tags(title[0].strip()):
            print 'title =',filter_tags(title[0].strip())
            fp.write(filter_tags(title[0].strip()))
            fp.write('\r\n')
        else:
            fp.write('&')
            fp.write('\r\n')
        if filter_tags(des[0].strip()):
            print 'des =',filter_tags(des[0].strip())
            fp.write(filter_tags(des[0].strip()))
            fp.write('\r\n')
        else:
            fp.write('&')
            fp.write('\r\n')
        a_num=0
        for ansl in ans:
            if filter_tags(ansl.strip()):
                a_num+=1
                an_two=filter_tags(ansl.strip())
                print 'ans =',an_two
                fp.write(an_two)
                fp.write('\r\n')
            else:
            	fp.write('&')
            	fp.write('\r\n')
        print "#####"
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值