下面是我的爬虫代码,
class Blurb2Spider(BaseSpider):
name = "blurb2"
allowed_domains = ["www.domain.com"]
def start_requests(self):
yield self.make_requests_from_url("http://www.domain.com/bookstore/new")
def parse(self, response):
hxs = HtmlXPathSelector(response)
urls = hxs.select('//div[@class="bookListingBookTitle"]/a/@href').extract()
for i in urls:
yield Request(urlparse.urljoin('www.domain.com/', i[1:]),callback=self.parse_url)
def parse_url(self, response):
hxs = HtmlXPathSelector(response)
print response,'------->'
我在这里尝试将 href 链接与基本链接结合起来,但出现以下错误,
exceptions.ValueError: Missing scheme in request url: www.domain.com//bookstore/detail/3271993?alt=Something+I+Had+To+Do
谁能告诉我为什么会出现此错误以及如何将基本 url 与 href 链接连接并产生请求
最佳答案
另一种解决方案,如果您不想使用 urlparse
:
response.urljoin(i[1:])
这个解决方案更进一步:在这里 Scrapy 计算出用于加入的域基础。如您所见,您不必提供明显的 http://www.example.com
即可加入。
如果您想更改要抓取的域,这将使您的代码在将来可重用。
关于python - 在 scrapy 中将基本 url 与结果 href 结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10798118/