AttributeError: 'str' object has no attribute 'xpath' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: AttributeError: 'str' object has no attribute 'xpath' (/thread-22380.html) |
AttributeError: 'str' object has no attribute 'xpath' - nazmulfinance - Nov-10-2019 Dear Members, I am sorry for the repeated post. I posted on the wrong sub-forum and then realized that this sub-forum is the right one for me. I am sorry for the inconvenience. I am very new to python. I am using the following code to extract the details of each product. I am collecting product names from the original page and using each product link, I am collecting price, SKU, and frame information from the linked pages. However, I am getting an attribute error for the web-link command. One possible problem could be that the parsed link includes "https://www.glassesshop.com". I think the main problem is href="https://www.glassesshop.com/eyeglasses/fz1370". The code would work if the link were href="/eyeglasses/fz1370" Full python code: import scrapy class GlassSpider(scrapy.Spider): name = 'glass' allowed_domains = ['www.glassesshop.com'] start_urls = ['https://www.glassesshop.com/bestsellers/'] def parse(self, response): names=response.xpath("//p[@class='pname col-sm-12']/a") for name in names: name=name.xpath(".//text()").get() link=name.xpath(".//@href").get() yield response.follow(url=link, callback=self.parse_glass, meta={'glass_name': name}) def parse_glass(self, response): name=response.request.meta['glass_name'] price=response.xpath("//span[@class='product-price-original']/text()").get() sku=response.xpath("//ul[@class='col-12 col-sm-6 default-content']/li[1]/text()").get() frame=response.xpath("//a[@class='col01']/text()").get() yield{ 'glass_name': name, 'price': price, 'sku': sku, 'frame': frame }I started working on python this week. I would highly appreciate if you can mention what specific change I should make to my codes to correct the attribute error |