Python 属性错误:';str';对象没有属性';xpath';
使用Python3、Scrapy1.7.3 下面使用下面的链接 但它给了我AttributeError的错误:“str”对象没有属性“xpath”Python 属性错误:';str';对象没有属性';xpath';,python,xpath,scrapy,Python,Xpath,Scrapy,使用Python3、Scrapy1.7.3 下面使用下面的链接 但它给了我AttributeError的错误:“str”对象没有属性“xpath” <table border="1" cellspacing="0" class="GridViewStyle" id="ctl00_BodyContents_subheading_gridview" rules="all" style="border-collapse:collapse;"> <tbody><tr
<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>
创建temp dict并产生其价值
我得到的错误是
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
一旦您提取它,它就会变成一个字符串,这样库就不能再处理它了。我相信您需要这样的东西(注意我是如何使用相对的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
for row in response.xpath('//tr[contains(@class,"GridView")][position() > 1]'):
temp=dict()
temp['Code'] = row.xpath('.//td[1]/a/text()').extract_first() # may be you need .extract() here
temp['Desc'] = row.xpath('.//td[2]/a/text()').extract_first() # may be you need .extract() here
yield temp