废料输出问题

2024-09-29 01:37:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我在显示我想要的项目时遇到问题。我的代码如下:

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.http import request
from scrapy.selector import HtmlXPathSelector
from texashealth.items import TexashealthItem

class texashealthspider(CrawlSpider):

    name="texashealth"
    allowed_domains=['jobs.texashealth.org']
    start_urls=['http://jobs.texashealth.org/search/?&q=&title=Filter%3A%20title&facility=Filter%3A%20facility&location=Filter%3A%20city&date=Filter%3A%20date']

    rules=(
    Rule(SgmlLinkExtractor(allow=("search/",)), callback="parse_health", follow=True),
    #Rule(SgmlLinkExtractor(allow=("startrow=\d",)),callback="parse_health",follow=True),
    )

    def parse_health(self, response):
        hxs=HtmlXPathSelector(response)
    titles=hxs.select('//tbody/tr/td')
    items = []

    for titles in titles:
        item=TexashealthItem()
        item['title']=titles.select('span[@class="jobTitle"]/a/text()').extract()
        item['link']=titles.select('span[@class="jobTitle"]/a/@href').extract()
        item['shifttype']=titles.select('span[@class="jobShiftType"]/text()').extract()
        item['location']=titles.select('span[@class="jobLocation"]/text()').extract()
        items.append(item)
    print items
    return items

显示的输出格式如下:

^{pr2}$

正如您在上面看到的,这些项的参数以不同的间隔显示,也就是说,它在一行中显示标题和链接,而在其他单独的行中显示其余的输出。在

我能得到一个解决方案,这样我就可以在一个镜头中显示所有的参数吗?在

谢谢你的帮助


Tags: fromimportparseextractitemsitemruleselect
1条回答
网友
1楼 · 发布于 2024-09-29 01:37:44

应该循环表行tr元素,而不是表单元格td元素。在

我建议您使用hxs.select('//table[@id="searchresults"]/tbody/tr'),然后在每个循环迭代中使用.//span...

titles=hxs.select('//table[@id="searchresults"]/tbody/tr')
items = []
for titles in titles:
    item['title']=titles.select('.//span[@class="jobTitle"]/a/text()').extract()
    item['link']=titles.select('.//span[@class="jobTitle"]/a/@href').extract()
    item['shifttype']=titles.select('.//span[@class="jobShiftType"]/text()').extract()
    item['location']=titles.select('.//span[@class="jobLocation"]/text()').extract()
    items.append(item)
return items

相关问题 更多 >