【发布时间】:2020-10-12 12:27:05
【问题描述】:
我正在学习使用 Scrapy 进行抓取,并且在某些代码中遇到了一些问题,这给了我一个我不理解的奇怪输出。有人可以向我解释为什么我得到一堆“\r\n\t\t\t\t\t\t\t”
我在堆栈溢出中找到了这个解决方案: Remove an '\\n\\t\\t\\t'-element from list
但我想知道是什么原因造成的。
这是导致我的问题的代码。上面链接中的 Strip 方法解决了它,但如前所述,我不明白它来自哪里。
import scrapy
import logging
import re
class CitySpider(scrapy.Spider):
name = 'city'
allowed_domains = ['www.a-tembo.nl']
start_urls = ['https://www.a-tembo.nl/themas/category/city/']
def parse(self, response):
titles = response.xpath("//div[@class='hikashop_category_image']/a")
for title in titles:
series = title.xpath(".//@title").get()
link = title.xpath(".//@href").get()
#absolute_url = f"https://www.a-tembo.nl{link}"
#absolute_url = response.urljoin(link)
yield response.follow(link, callback=self.parse_title)
def parse_title(self, response):
rows = response.xpath("//table[@class='hikashop_products_table adminlist table']/tbody/tr")
for row in rows:
product_code = row.xpath(".//span[@class='hikashop_product_code']/text()").get()
product_name = row.xpath(".//span[@class='hikashop_product_name']/a/text()").get()
yield{
"Product_code": product_code,
"Product_name": product_name
}
【问题讨论】:
标签: python python-3.x web-scraping scrapy scrapy-shell