当我尝试添加单词列表时,我收到此错误

时间:2016-08-04 14:18:38

标签: python django list django-taggit

我正在尝试在我的django应用中为我的代码添加单词列表。但是我收到以下错误

Cannot add ['ct', 'panty'] (<class 'list'>). Expected <class 'django.db.models.base.ModelBase'> or str.

我有抓不同网站的功能,我希望每个网站都有不同的标签。我最初使用了像这样的if语句

        if entry['name'] == 'pan':
            post.tags.add("Musica", "video")
        elif entry['name'] == 'world':
            post.tags.add("video")
        else:
            post.tags = None

但是我认为如果我每次刮痧都有不同的标签会更好。我想如果使用elif,否则会减慢一个已经很慢的过程。这是我的代码

def panties():
    from lxml import html
    pan_url = 'http://www.pan.org'
    shtml = requests.get(pan_url, headers=headers)
    soup = BeautifulSoup(shtml.text, 'html5lib')
    video_row = soup.find_all('div', {'class': 'video'})
    name = 'pan'
    tag_list = ['ct', 'panty']
    tags = [word for word in tag_list]
    author = User.objects.get(id=1)

    def youtube_link(url):
        youtube_page = requests.get(url, headers=headers)
        soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
        video_row = soupdata.find_all('script', {'type': 'text/javascript'})
        entries = [{'text': str(div),
                    } for div in video_row]

        tubby = str(entries[4])
        urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
        return urls

    def embed(url):
        new_embed = url.replace("watch?v=", "embed/")
        return new_embed

    entries = [{'href': div.a.get('href'),
                'src': youtube_link(div.a.get('href'))[1],
                'text': div.h4.text,
                'comments': div.h4.text,
                'name': name,
                'url': div.a.get('href'),
                'embed': embed(youtube_link(div.a.get('href'))[0]),
                'author': author,
                'video': True,
                'tags': tags
                } for div in video_row][:13]
         tags = saved_post.tags.add("Musica", "video")
    return entries




def save_the_scrapes():
    one = panties()
    two = other()
    mergence = one + two
    random.shuffle(mergence)

    for entry in mergence:
        post = Post()
        post.title = entry['text']
        title = post.title
        if not Post.objects.filter(title=title):
            post.title = entry['text']
            post.name = entry['name']
            post.url = entry['url']
            post.body = entry['comments']
            post.image_url = entry['src']
            post.video_path = entry['embed']
            post.author = entry['author']
            post.video = entry['video']
            post.status = 'draft'
            post.save()

            if entry['name'] == 'pan videos':
                post.tags.add("Musica", "video")
            elif entry['name'] == 'world star':
                post.tags.add("video")
            else:
                post.tags = None


    return mergence

我也尝试过标签。

tags = " 'tags', 'cat' "

但这也不起作用。因为django taggit只是让这两个链接。我正在通过django学习python,所以我对这门语言的知识存在差距。任何帮助将不胜感激。

0 个答案:

没有答案