当前位置: 首页>>代码示例>>Python>>正文


Python Scraper.scrapeSitename方法代码示例

本文整理汇总了Python中scraper.Scraper.scrapeSitename方法的典型用法代码示例。如果您正苦于以下问题:Python Scraper.scrapeSitename方法的具体用法?Python Scraper.scrapeSitename怎么用?Python Scraper.scrapeSitename使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scraper.Scraper的用法示例。


在下文中一共展示了Scraper.scrapeSitename方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_article

# 需要导入模块: from scraper import Scraper [as 别名]
# 或者: from scraper.Scraper import scrapeSitename [as 别名]
def create_article(request):


	if request.method == 'POST':
		
		form = ArticleForm(request.POST)

		if form.is_valid():

			if Article.objects.filter(user = request.user, url = form.cleaned_data['url']):
				print >> sys.stderr, "Already posted"
				response_data = {'Error': 'You have already posted this article'}
				return HttpResponse(
					json.dumps(response_data),
					content_type="application/json"
				)
				
			
			try:
				
				#Create new article
				new_article = form.save(commit=False)

				cleaner = urlparse(new_article.url)
				cleaned_url = "http://" + cleaner.netloc + cleaner.path			
				new_article.url = cleaned_url

				# scape info from URL
				scraper = Scraper(new_article.url)
				new_article.user = request.user
				new_article.image, new_article.image_url = scraper.scrapeImage()
				new_article.title = scraper.scrapeTitle()
				new_article.site_name = scraper.scrapeSitename()
				new_article.description = scraper.scrapeDescr()
				new_article.pub_date = date.today()
				new_article.real_pub_date = datetime.datetime.today()
				new_article.save()


				# adjust date
				article_date = datetime.datetime.strptime(str(new_article.pub_date), '%Y-%m-%d').strftime('%b %d, %Y')

				response_data = {
					'article_id': new_article.id,
					'article_url': new_article.url,
					'article_image': new_article.image.url,
					'article_title': new_article.title,
					'article_site_name': new_article.site_name,
					'article_user': new_article.user.username,
					'article_user_id': new_article.user.id,
					'article_description': new_article.description,
					'article_pub_date': article_date,
				}

				# Success
				return HttpResponse(
					json.dumps(response_data),
					content_type="application/json"
				)
				
			except:
				response_data = {'Error': 'Error posting link. Please try again'}
				return HttpResponse(
					json.dumps(response_data),
					content_type="application/json"
				)
			

	response_data = {'Error': 'Error posting link. Please try again'}
	return HttpResponse(
		json.dumps(response_data),
		content_type="application/json"
	)
开发者ID:ksj1993,项目名称:django_newsapp,代码行数:75,代码来源:views.py


注:本文中的scraper.Scraper.scrapeSitename方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。