Error: pyMySQL is not working in spiders of scrapy

Error: pyMySQL is not working in spiders of scrapy

当我在 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

检查是否安装了 PyMySQL 模块:

python -c "import pymysql"

如果一切正常,这个命令应该returns什么都没有。

如果它 returns 一个 ModuleNotFoundError,那么 install the module 通过使用 pip:

pip install PyMySQL