【问题标题】:Error: pyMySQL is not working in spiders of scrapy错误:pyMySQL 在爬虫的蜘蛛中不起作用
【发布时间】:2018-12-02 13:23:07
【问题描述】:

当我在 python 的 scrapy 项目中导入 pyMysql 库时,它会给出一个错误,即找不到模块。我想问如何在scrapy项目的python文件中导入pyMysql库。当我在简单的 python 中导入 pyMySQL 时,它工作正常。

在由命令“genspider spider_name (url)”生成的蜘蛛中,我使用了这个给出错误的代码。

enter code here

import scrapy
from amazon.items import AmazonItem
import pymysql


class AmazonProductSpider(scrapy.Spider):
    name = "AmazonDeals"
    allowed_domains = ["amazon.com"]

# Use working product URL below
start_urls = [
    "https://www.amazon.com/gp/product/B01IO0QWJA","https://www.amazon.in/Mi-Redmi-5-Gold-32GB/dp/B0756RF9KY"
]

def parse(self, response):
    items = AmazonItem()
    title = response.xpath('//h1[@id="title"]/span/text()').extract()
    sale_price = response.xpath('//span[contains(@id,"ourprice") or contains(@id,"saleprice")]/text()').extract()
    category = response.xpath('//a[@class="a-link-normal a-color-tertiary"]/text()').extract()
    availability = response.xpath('//div[@id="availability"]//text()').extract()
    items['product_name'] = ''.join(title).strip()
    items['product_sale_price'] = ''.join(sale_price).strip()
    items['product_category'] = ','.join(map(lambda x: x.strip(), category)).strip()
    items['product_availability'] = ''.join(availability).strip()
    yield items

【问题讨论】:

    标签: python-3.x scrapy scrapy-spider pymysql


    【解决方案1】:

    检查PyMySQL 模块是否已安装:

    python -c "import pymysql"
    

    如果一切正常,此命令不应返回任何内容。

    如果它返回一个ModuleNotFoundError,那么install the module使用pip:

    pip install PyMySQL
    

    【讨论】:

    • 它已经安装并在简单的python程序中运行
    • 你能提供一些源代码来重现这个问题吗?
    • 问题可能来自您的环境。如果我尝试直接从我的 shell 执行你的脚本,我只会得到你的错误。如果我使用 python [scriptfilename] 执行脚本,一切正常。
    • 但是当我使用 python [scrptfilename] 执行时,它给了我错误,找不到 scrapy 模块
    • 你使用 conda 吗?
    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2010-12-03
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多