我有一個python腳本,可以從指定的站點中提取鏈接,並且我想用Django製作一個單獨的webapp。這是我的第一個Python和Django項目,所以我希望它儘可能簡單,以避免任何狡猾,但我有點卡住。Python/Django列表對象沒有屬性'status_code'
這是我的views.py
from django.http import HttpResponse
from bs4 import BeautifulSoup
import urllib2, sys
import urlparse
import re
import time, threading
def businessghana(request):
site = "http://www.businessghana.com/portal/jobs"
hdr = {'User-Agent' : 'Mozilla/5.0'}
req = urllib2.Request(site, headers=hdr)
jobpass = urllib2.urlopen(req)
soup = BeautifulSoup(jobpass)
for tag in soup.find_all('a', href = True):
tag['href'] = urlparse.urljoin('http://www.businessghana.com/portal/', tag['href'])
return map(str, soup.find_all('a', href = re.compile('.getJobInfo')))
threading.Timer(3600, businessghana).start()
有這些功能的10個不同地點約10,但它們是類似性質的這樣的Django項目,我只用1其中之一。同樣,爲了保持它的簡單,避免fustration
這是我的urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^hello/$', 'listing.views.businessghana'),
url(r'^admin/', include(admin.site.urls)),
)
這是我的models.py:
(我覺得我廢話噴涌,但是這是最好的我能想出)
class jobLinks(models.Model):
links = models.CharField(max_length = 200)
pub_date = models.DateTimeField('date retrieved')
def __unicode__(self):
return self.links
當我訪問的網址:
127.0.0.1:8000/hello/
我得到錯誤'list'對象沒有屬性'status_code'。
這是一個單頁面站點,它只顯示從視圖中的函數中檢索的鏈接。我還有其他計劃讓它更復雜一些,但我想從這開始。
請問我錯過了什麼?
models.py內容是否有意義?
在此先感謝
您沒有引用的模型任何區域,所以它沒有什麼區別是否使任何意義或不。 (另一方面,threading.Timer調用完全沒有意義。) –
我希望threading.Timer調用將讓函數以指定的間隔運行。是否有更好的方法來實現這種行爲,我在IDLE中嘗試過它,但它的功能正如預期的那樣 – fromPythonImportNoob
那麼假設它再次調用函數,那麼期望返回值會發生什麼?他們會去哪裏? –