我正在尝试将抓取的数据保存在Postgres数据库中。 我想为此使用django模型。
我以前尝试过使用Psycopg2软件包,但是我发现这是不必要的,所以我决定只使用Django模型。 当我使用Psycopg2软件包时,数据也没有进入数据库。
我收到此错误:
Traceback (most recent call last):
File "/home/xxxx/Desktop/project/django/tnapp/scrap.py", line 61, in <module>
scraped_author = author(name='author name')
TypeError: 'NoneType' object is not callable
刮板:
import requests
from bs4 import BeautifulSoup as bs
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from collections import Counter
import psycopg2
# from sqlalchemy.dialects.postgresql import psycopg2
url = 'https://teonite.com/blog/page/{}/index.html'
all_links = []
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'User-Agent': 'Mozilla/5.0'
}
with requests.Session() as s:
r = s.get('https://teonite.com/blog/')
soup = bs(r.content, 'lxml')
article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
all_links.append(article_links)
num_pages = int(soup.select_one('.page-number').text.split('/')[1])
for page in range(2, num_pages + 1):
r = s.get(url.format(page))
soup = bs(r.content, 'lxml')
article_links = ['https://teonite.com' + item['href'][2:] for item in soup.select('.post-content a')]
all_links.append(article_links)
all_links = [item for i in all_links for item in i]
d = webdriver.Chrome(ChromeDriverManager().install())
contents = []
authors = []
for article in all_links:
d.get(article)
soup = bs(d.page_source, 'lxml')
[t.extract() for t in soup(['style', 'script', '[document]', 'head', 'title'])]
visible_text = soup.getText()
content = soup.find('section', attrs={'class': 'post-content'})
contents.append(content)
author = soup.find('span', attrs={'class': 'author-content'})
authors.append(author)
unique_authors = list(set(authors))
unique_contents = list(set(contents))
try:
print(soup.select_one('.post-title').text)
except:
print(article)
print(soup.select_one('h1').text)
break # for debugging
d.quit()
scraped_author = author(name='author name')
author.save()
型号:
from django.db import models
class author(models.Model):
author_id = models.CharField(primary_key=True, max_length=50, editable=False)
author_name = models.CharField(max_length=50)
class Meta:
ordering = ['-author_id']
db_table = 'author'
class stats(models.Model):
content = models.CharField(max_length=50)
stats = models.IntegerField()
class Meta:
ordering = ['-stats']
db_table = 'stats'
class authorStats(models.Model):
author_id = models.CharField(max_length=100)
content = models.CharField(max_length=100)
stats = models.IntegerField()
class Meta:
ordering = ['stats']
db_table = 'author_stats'
答案 0 :(得分:1)
您有两件事叫做author
;您的模型以及在抓取的内容中找到的值。
如果您遵循Python样式约定,则不会发生。您应该始终给类(包括模型)以大写字母开头的名称。您的模型应该命名为Author
,而不是author
(以及其他模型Stats
和AuthorStats
)。