没有news这个模块
不知道诸位能不能看得到附件,我在学习爬虫爬网易新闻时,怎么运行文件都出现这句:No module named 'news'import scrapy
from news.items import NewsItem
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
import time
#https://www.163.com/dy/article/G6LL83J3051481US.html
#https://www.163.com/dy/article/G6JQ3TLB055040N3.html
#https://news.163.com/21/0403/16/G6M23BAK000189FH.html
#https://news.163.com/21/0403/16/G6M25RJH000189FH.html
#https://news.163.com/21/0402/11/G6ITTCLV0001899O.html
#https://news.163.com/21/0402/11/G6IT93GF0001899O.html
class News163Spider(CrawlSpider,get_time):
name = 'news163'
allowed_domains = ['news.163.com']
start_urls = ['http://news.163.com/']
rules = (
Rule(LinkExtractor(allow=r'https://news.163.com/21/0402/\d+/.*?.html'),callback='parse_item',follow=True),
)
def parse_item(self, response):
item = NewsItem()
item['news_thread']=response.url.strip().split('/')[-1][:-5]
self.get_title(response,item)
self.get_time(response,item)
self.get_source(response,item)
self.get_source_url(response,item)
self.get_text(response,item)
self.get_url(response,item)
return item
def get_url(self,response,item):
url=response.url
if url:
print('news_url:{}'.format(url))
item['news_url']=url
def get_text(self,response,item):
text=response.css('.post_body p::text').extract()
if text:
print('text:{}'.format(text))
item['news_body']=text
def get_source_url(self,response,item):
source_url=response.css('content::attr(href)').extract()
if source_url:
print('source_url:{}'.format(source_url))
item['source_url']=source_url
def get_source(self,response,item):
source=response.css('content::text').extract()
if source:
print('source:{}'.format(source))
item['news_source']=source
def get_time(self,response,item):
time=response.css('div.post_info::text').extract()
if time:
print('time:{}'.format(time.strip().replace('来源:','')))
item['news_time']=time.strip().replace('来源:','')
def get_title(self,response,item):
title=response.css('titel::text').extract()
print('*'*10)
if title:
print('title:{}'.format(title))
item['news_title']=title
然后运行出现:Traceback (most recent call last):
File "C:\news\news\spiders\news163.py", line 2, in <module>
from news.items import NewsItem
ModuleNotFoundError: No module named 'news' 你都没下载这个news模块吧 在cmd里输入
pip install news 本帖最后由 Stubborn 于 2021-4-5 19:52 编辑
从项目下面开始导包,有可能是启动位置不对
例如我的爬虫项目路径>>I:\Project\Template\ScrapyMode\items.py
爬虫文件路径>>I:\Project\Template\ScrapyMode\spiders\**.py
则在爬虫文件导入item字典是,应该是这样的
from ScrapyMode.items import **Item
从你的路径看来
File "C:\news\news\spiders\news163.py", line 2, in <module>,尝试从news导入item
from news.items import **items
看下是否是启动路径问题
在项目文件下面,新建一个py文件。(和scrapy.cfg这个文件同级),键入如下代码: 爬虫名字不一样,记得修改
# -*- coding: utf-8 -*-
# !/usr/bin/python3
"""
Created on 10月/16/2019-->Bless no BUG<--</00:16:45
@author: 1263270345@qq.com / Alex
"""
from scrapy.cmdline import execute
execute(['scrapy', 'crawl', 'Douban']) yayc_zcyd 发表于 2021-4-5 19:00
你都没下载这个news模块吧
我有啦!
Requirement already satisfied: news in c:\users\win10\appdata\local\programs\python\python38\lib\site-packages (1.0) yayc_zcyd 发表于 2021-4-5 19:01
在cmd里输入
我有装的。 Stubborn 发表于 2021-4-5 19:34
从项目下面开始导包,有可能是启动位置不对
例如我的爬虫项目路径>>I:\Project\Template\ScrapyMode\items ...
{:5_96:}不行呢~凄凄惨惨戚戚...... 你的Spider继承get_time,是什么梗,哪里来的,每天听过要继承这个类啊,这次和上次报错不一样勒
页:
[1]