Now, I develop web app with django and scrapy that has function of searching English word. When I input word with form on django, scrapy get the mean of transltated word into Japanese from internet dictionary.
After inputting word ,activating scrapy and stop runserver, happning the error following:
[scrapy.crawler] INFO: Received SIGINT, shutting down gracefully. Send again to force
Error: That port is already in use.
I can stop it by using command kill , but it is inconvinient to input command everytime happens error. So I want to know how to solve it like adding code etc.
following is my code .
def add_venue(request):
submitted = False
if request.method =='POST':
form = VenueForm(request.POST)
if form.is_valid():
name = form.cleaned_data['word']#入力した単語
queryset = Newword.objects.filter(word = name)
print(queryset)
if queryset.exists():
#コード未記入
else:
#print(name)
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(WordSpider, query=name)#入力した単語をnameに入れて、スクレイピングを実行。
process.start() # the script will block here until the crawling is finished
form.save()
return HttpResponseRedirect('/list/')
else:
form = VenueForm
if 'submitted' in request.GET:
submitted = True
return render(request, 'form.html', {'form':form, 'submitted':submitted})
python3 manage.py runserver --nothreading --noreload
class WordSpider(scrapy.Spider):
name = 'word'
allowed_domains = ['ejje.weblio.jp']
# start_urls = ['http://ejje.weblio.jp/content/']
def __init__(self, query='', *args, **kwargs):
super(WordSpider, self).__init__(*args, **kwargs)
#self.user_agent = 'custom-user-agent'
self.start_urls = ['https://ejje.weblio.jp/content/' + query]
def parse(self, response):
# item=ElscrapyItem()
# item['word']=response.xpath('//*[@id="summary"]/div[2]/p/span[2]/text()').get().replace('\n', '').strip()
# yield item
word=response.xpath('//*[@id="summary"]/div[2]/p/span[2]/text()').get().replace('\n', '').strip()
#loader = ItemLoader(item = ElscrapyItem(), response=response)
#loader.add_xpath('word', '//*[@id="summary"]/div[2]/p/span[2]/text()')
#yield loader.load_item()
yield{
'word':word
}
django project is in scrapy project.