AttributeError: 'str' 对象没有属性 'xpath' [英] AttributeError: 'str' object has no attribute 'xpath'

查看:85
本文介绍了AttributeError: 'str' 对象没有属性 'xpath'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 3,Scrapy 1.7.3使用以下链接Scrapy - 从表中提取项目

Using Python 3,Scrapy 1.7.3 to Following using following link Scrapy - Extract items from table

但它给了我 AttributeError 的错误:'str' object has no attribute 'xpath'

but it is giving me error of AttributeError: 'str' object has no attribute 'xpath'

    <table border="1" cellspacing="0" class="GridViewStyle" id="ctl00_BodyContents_subheading_gridview" rules="all" style="border-collapse:collapse;">
<tbody><tr class="GridViewHeaderStyle" style="background-color:#66B6F4;">
<th scope="col">
<span id="ctl00_BodyContents_subheading_gridview_ctl01_SUBHEADING_CODES_HEADING" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">HS-Code</span>
</th><th scope="col">
<span id="ctl00_BodyContents_subheading_gridview_ctl01_SUBHEADING_DESCRIPTION_HEADING" style="padding:20px 20px 20px 5px;font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;margin:2px">Item Description</span>
</th>
</tr><tr class="GridViewRowStyle">
<td style="width:15%;">
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl02_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td style="width:85%;">
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl02_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr><tr class="GridViewAlternatingRowStyle">
<td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl03_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl03_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr><tr class="GridViewRowStyle">
<td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl04_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl04_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr><tr class="GridViewAlternatingRowStyle">
<td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl05_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl05_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr><tr class="GridViewRowStyle">
<td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl06_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl06_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr><tr class="GridViewAlternatingRowStyle">
<td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl07_SUBHEADING_CODES" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td><td>
<a href="http://link.domain" id="ctl00_BodyContents_subheading_gridview_ctl07_SUBHEADING_DESCRIPTION" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif !important;font-size: 14px;">value1</a>
</td>
</tr>
</tbody></table>

爬虫代码

# -*- coding: utf-8 -*-
import scrapy
class CybexbotSpider(scrapy.Spider): 
   name = 'cybexbot'
   allowed_domains = ['http://links.com']
   start_urls = ['http://links.com']
   def parse(self, response):
       data=response.xpath('//tr[contains(@class,"GridView")]').extract()
       for d in data[1:]:
         print(type(d))
         temp=dict()
         temp['Code']=d.xpath('tr//td[1]/a/text()').extract()
         temp['Desc']=d.xpath('tr//td[2]/a/text()').extract()
         yield temp

创建临时字典并产生它的值

created temp dict and yield its value

我得到的错误是

  temp['Code']=d.xpath('tr//td[1]/a/text()').extract()
AttributeError: 'str' object has no attribute 'xpath'

推荐答案

试试这个:

import scrapy
class CybexbotSpider(scrapy.Spider): 
   name = 'cybexbot'
   allowed_domains = ['http://links.com']
   start_urls = ['http://links.com']
   def parse(self, response):
       data=response.xpath('//tr[contains(@class,"GridView")]')
       for d in data[1:]:
         print(type(d))
         temp=dict()
         temp['Code']=d.xpath('tr//td[1]/a/text()').extract()
         temp['Desc']=d.xpath('tr//td[2]/a/text()').extract()
         yield temp

一旦你提取它,它就会变成一个字符串,所以图书馆不能再处理它

Once you extract it, it becomes a string so the library can no longer process it

这篇关于AttributeError: 'str' 对象没有属性 'xpath'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆