python - 在 scrapy 网络爬虫中获取错误

标签 python web-scraping scrapy web-crawler scrapy-spider

您好,我尝试在我的代码中实现它。但是我收到以下错误:exceptions.NameError: global name 'Request' is not defined

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector 
from bs4 import BeautifulSoup

class spider_aicte(BaseSpider):
    name = "Indian_Colleges"
    allowed_domains = ["http://www.domain.org"]
    start_urls = [
        "http://www.domain.org/appwebsite.html",
        ]

    def parse(self, response):
        filename = response.url.split("/")[-2]
        soup = BeautifulSoup(response.body)
        for link in soup.find_all('a'):
            download_link = link.get('href')
            if '.pdf' in download_link:
                pdf_link = "http://www.domain.org" + download_link
                print pdf_link
                class FileSpider(BaseSpider):
                    name = "fspider"
                    allowed_domains = ["www.domain.org"]
                    start_urls = [
                            pdf_link
                            ]
        for url in pdf_link:
            yield Request(url, callback=self.save_pdf)

    def save_pdf(self, response):
         path = self.get_path(response.url)
         with open(path, "wb") as f:
            f.write(response.body)

最佳答案

您应该导入 Request使用前:

from scrapy.http import Request

或者,还有一个“捷径”导入:

from scrapy import Request

或者,如果您有 import scrapy 行,请使用 scrapy.Request

关于python - 在 scrapy 网络爬虫中获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16139846/

相关文章:

python - Scrapy正在爬取,但没有输出

Python 记录器不遵守设置的级别

javascript - 当我使用 Nightmare 时,在页面之间移动并进行抓取

python - 动态设置scrapy请求回调

python - 使用python从图表中提取数据

javascript - 怎么打开网站隐藏的信息

curl - Scrapyd:一旦我使用 scrapyd 安排它,我在哪里可以看到我的爬虫的输出

python - Gunicorn 工作线程在请求​​后不会压缩内存

python - 将此字符串格式化为列表的最简单但不是最快的方法是什么? Python

python - Popen 不再适用于 apache/wsgi 和 python 2.7.2?