python - 在 scrapy 中将基本 url 与结果 href 结合起来

标签 python url scrapy

下面是我的爬虫代码,

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/

相关文章:

python - undefined variable : SerialException

python - Pandas 选择 n 中间行

python - Pandaic 方法检查数据框是否有任何行

Android从浏览器获取url

python - 如何使用css选择器获取跨度数据?

image - Strapi:通过重命名默认图像名称来自定义图像管道

python - 用于 Python 捕获模块隐私提示的静态代码分析器

java - 获取 java.io.IOException : Error writing to server at getInputStream

iOS App Store URL - 评分按钮 - iOS

html - 无法在 Scrapy 中使用 XPath 访问 HTML 元素