Reputation: 65
Using Python 3,Scrapy 1.7.3 to Following using following link Scrapy - Extract items from table
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>
Scrapy code
# -*- 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
error i am getting is
temp['Code']=d.xpath('tr//td[1]/a/text()').extract()
AttributeError: 'str' object has no attribute 'xpath'
Upvotes: 3
Views: 7675
Reputation: 10666
I believe you need something like this (notice how I use relative XPath to get values):
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
Upvotes: 1
Reputation: 6748
Try this:
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
Upvotes: 4