首页 > python新手求帮助

python新手求帮助

-- coding: utf-8 --

import scrapy
import codecs
import re

class SinaSpider(scrapy.Spider):

name = "sina"
fileout = codecs.open('sina.txt', 'a', 'utf-8')
allowed_domains = ["sina.cn"]
start_urls = (
    'http://sports.sina.cn/?vt=4&pos=108&vs=3',
)

def parse(self, response):
     type_list = response.xpath('//div[@class="carditems"]//a/@href').extract()
     global url,i
     for i in type_list:
        url=i
        print url
        yield scrapy.Request(i, callback=self.parse_item)
def parse_item(self,response):
    t= response.xpath('//section[@class="art_main_card j_article_main"]//h1//text()').extract()[0]
    strinfo = re.compile('\s')
    title=strinfo.sub('',t)
    leixing1=response.xpath('//nav[@class="sinaHead"]//li//text()').extract()[0]
    strinfo = re.compile('\s')
    type1=strinfo.sub('',leixing1)
    leixing2=response.xpath('//nav[@class="sinaHead"]//li//text()').extract()[1]
    strinfo = re.compile('\s')
    type2=strinfo.sub('',leixing2)
    type= type1 + '' + type2
    type2_list = response.xpath('//div[@class="comment-count"]//a//@href').extract()
    for b in type2_list:
         print b
         yield scrapy.Request(b,callback=self.parse2_item)
    self.fileout.write(
         title + '\001' + type + '\001' + comment
    )
    self.fileout.write('\n')
def parse2_item(self,response):
    global comment
    comment=response.xpath('//p[@class="center_tips"]//p//text()').extract()[0]
    

"E:\Program Files (x86)\python27\python.exe" E:/study/CX/python/rexx/sina.py sina

Process finished with exit code 0
这个问题怎么解决

这个爬虫想实现的爬去数据是将URL,也就是在parse中的i输出到文件中,并且将每个URL需要爬去的内容爬去出来。我现在爬去到的只是一个相同的URL,希望大神们帮忙解决


你的xpath写的不对,

'//div[@class="carditems"]//a/@href'

第二个//会导致从头开始搜索,正确的方法是使用descendant


Process finished with exit code 0

意思是,程序正常地运行完毕了。

纵观你没有良好排版的代码,你只是定义一个类,而并没有使用它。如果你做好了美味的饭菜而你并不吃,你当然还是会挨饿。

【热门文章】
【热门文章】