Unable to import scrapy

六月ゝ 毕业季﹏ 提交于 2020-06-21 05:42:02

问题


I am trying to scrape a website. However, I get this error inside the VS Code editor:

Unable to import 'scrapy'pylint(import-error)

Unable to import 'scrapy'pylint(import-error) These are the important parts of codes I am using, I just didn't copy all the variables I am scraping:

 class xxxxxxxxSpider(scrapy.Spider):

name = 'xxxxxxx'
allowed_domains = 
['www.drsaina.com/ConsultationDoctor/%D9%85%D8%B4%D8%A7%D9%88%D8%B1%D9%87- 
 %D8%A2%D9%86%D9%84%D8%A7%DB%8C%D9%86']

start_urls =['https://www.drsaina.com/ConsultationDoctor/%D9%85%D8%B4%D8%A7%D9%88%D8%B1%D9%87 
- 
 %D8%A2%D9%86%D9%84%D8%A7%DB%8C%D9%86/']

def parse(self, response):
    specialties = response.xpath("//h3").getall()
    for specialty in specialties:
        name = specialty.xpath(".//text()").getall()
        link = specialty.xpath(".//@href").getall()
        yield response.follow(url=link, callback=self.parse_spacialty, meta = 
{'specialty_name':name})

def parse_spacialty(self, response):
    specialty_name = response.request.meta['specialty_name']
    specifications = response.xpath 
("/html/body/main/div[5]/div/div/div[2]/div[2]/ul/li[1]/div/div")
    for specification in specifications:
        doctor_name = specification.xpath(".//div[1]/a[2]/span/text()").get()
        doctor_specialty = specification.xpath 
(".//div[1]/a[2]/label/text()").get()
yield{
            'specialty_name':specialty_name,
            'doctor_name':doctor_name,
            'doctor_specialty':doctor_specialty,
}

Also in prompt, when I run my crawler, this is the whole error I got (@gangabass):

2020-05-17 08:48:05 [scrapy.utils.log] INFO: Scrapy 2.1.0 started (bot: yyyyy)
2020-05-17 08:48:05 [scrapy.utils.log] INFO: Versions: lxml 4.5.0.0, libxml2 2.9
.5, cssselect 1.1.0, parsel 1.5.2, w3lib 1.21.0, Twisted 20.3.0, Python 3.7.7 (d
efault, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.1.0 (Op
enSSL 1.1.1g  21 Apr 2020), cryptography 2.9.2, Platform Windows-8.1-6.3.9600-SP
0
2020-05-17 08:48:05 [scrapy.utils.log] DEBUG: Using reactor: 
twisted.internet.se
lectreactor.SelectReactor
2020-05-17 08:48:05 [scrapy.crawler] INFO: Overridden settings:
{'BOT_NAME': 'yyyyy',
'NEWSPIDER_MODULE': 'yyyyy.spiders',
'ROBOTSTXT_OBEY': True,
'SPIDER_MODULES': ['yyyyy.spiders']}
2020-05-17 08:48:06 [scrapy.extensions.telnet] INFO: Telnet Password: 
6aefdde1ca
e354cd
2020-05-17 08:48:06 [scrapy.middleware] INFO: Enabled extensions:
 ['scrapy.extensions.corestats.CoreStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.logstats.LogStats']
 2020-05-17 08:48:07 [scrapy.middleware] INFO: Enabled downloader 
middleware:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
 2020-05-17 08:48:07 [scrapy.middleware] INFO: Enabled spider middleware:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2020-05-17 08:48:07 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2020-05-17 08:48:07 [scrapy.core.engine] INFO: Spider opened
2020-05-17 08:48:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 
0 page
es/min), scraped 0 items (at 0 items/min)
2020-05-17 08:48:07 [scrapy.extensions.telnet] INFO: Telnet console 
listening on
127.0.0.1:6023
2020-05-17 08:48:08 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
https://www.d
rsaina.com/robots.txt> (referer: None)
2020-05-17 08:48:09 [scrapy.core.engine] DEBUG: Crawled (200) <GET 
https://www.d
rsaina.com/ConsultationDoctor/%D9%85%D8%B4%D8%A7%D9%88%D8%B1%D9%87- 
%D8%A2%D9%86%
D9%84%D8%A7%DB%8C%D9%86/> (referer: None)
2020-05-17 08:48:09 [scrapy.core.scraper] ERROR: Spider error processing 
<GET https:// 
www.drsaina.com/ConsultationDoctor/%D9%85%D8%B4%D8%A7%D9%88%D8%B1%D9%87-%D
8%A2%D9%86%D9%84%D8%A7%DB%8C%D9%86/> (referer: None)
Traceback (most recent call last):
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
 packages\scrapy\uti
ls\defer.py", line 117, in iter_errback
    yield next(it)
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\uti
ls\python.py", line 345, in __next__
    return next(self.data)
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\uti
ls\python.py", line 345, in __next__
    return next(self.data)
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\cor
e\spidermw.py", line 64, in _evaluate_iterable
    for r in iterable:
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\spi
dermiddlewares\offsite.py", line 29, in process_spider_output
    for x in result:
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\cor
e\spidermw.py", line 64, in _evaluate_iterable
    for r in iterable:
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\spi
dermiddlewares\referer.py", line 338, in <genexpr>
    return (_set_referer(r) for r in result or ())
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\cor
e\spidermw.py", line 64, in _evaluate_iterable
    for r in iterable:
    File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
 packages\scrapy\spi
dermiddlewares\urllength.py", line 37, in <genexpr>
     return (r for r in result or () if _filter(r))
   File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
packages\scrapy\cor
e\spidermw.py", line 64, in _evaluate_iterable
     for r in iterable:
   File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
 packages\scrapy\spi
 dermiddlewares\depth.py", line 58, in <genexpr>
     return (r for r in result or () if _filter(r))
  File "c:\users\pc\anaconda3\envs\vitual_workspace\lib\site- 
 packages\scrapy\cor
  e\spidermw.py", line 64, in _evaluate_iterable
     for r in iterable:
    File "C:\Users\pc\projects\yyyyy\yyyyy\spiders\xxxxxxx.py", line 19, 
in parse
    name = specialty.xpath(".//text()").getall()
AttributeError: 'str' object has no attribute 'xpath'
2020-05-17 08:48:09 [scrapy.core.engine] INFO: Closing spider (finished)
2020-05-17 08:48:09 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 533,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 44999,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'elapsed_time_seconds': 1.774663,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2020, 5, 17, 4, 18, 9, 761477),
 'log_count/DEBUG': 2,
 'log_count/ERROR': 1,
 'log_count/INFO': 10,
 'response_received_count': 2,
 'robotstxt/request_count': 1,
 'robotstxt/response_count': 1,
 'robotstxt/response_status_count/200': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'spider_exceptions/AttributeError': 1,
 'start_time': datetime.datetime(2020, 5, 17, 4, 18, 7, 986814)}
2020-05-17 08:48:09 [scrapy.core.engine] INFO: Spider closed (finished)

Can someone help me out with this issue?

By the way, I am using the latest version I scrapy (2.1.0) and python 3

来源:https://stackoverflow.com/questions/61847198/unable-to-import-scrapy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!