|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
不知道诸位能不能看得到附件,我在学习爬虫爬网易新闻时,怎么运行文件都出现这句: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[0]))
item['source_url']=source_url[0]
def get_source(self,response,item):
source=response.css('content::text').extract()
if source:
print('source:{}'.format(source[0]))
item['news_source']=source[0]
def get_time(self,response,item):
time=response.css('div.post_info::text').extract()
if time:
print('time:{}'.format(time[0].strip().replace('来源:','')))
item['news_time']=time[0].strip().replace('来源:','')
def get_title(self,response,item):
title=response.css('titel::text').extract()
print('*'*10)
if title:
print('title:{}'.format(title[0]))
item['news_title']=title[0]
然后运行出现: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' |
-
|