【发布时间】:2019-04-18 08:25:57
【问题描述】:
我希望有一个网站,当用户输入内容时,他们会在网站上获得所有抓取数据视图..
现在这个 python3 代码可以通过 jupyter notebook 正常工作。 你们可以在 jupyter notebook 中测试它。但我想要以适当的方式用于用户界面。我知道 Django 可以与 python 代码集成,但我应该把这段代码放在哪里。是models.py吗?
from urllib.request import Request, urlopen as uReq
from bs4 import BeautifulSoup as soup
def make_soup(website) :
req = Request(website,headers = {'User-Agent' : 'Mozilla/5.0'})
uClient = uReq(req)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, 'html.parser')
return page_soup
google_news_url = 'https://www.google.com.my/search?q={}&source=Int&tbm=nws'
def forge_url (q):
return google_news_url.format(q.replace(' ','+'))
news_url = forge_url (input('Enter Search'))
website = make_soup(news_url)
headlines = website.findAll('h3')
n = 0
for item in headlines :
top = item.a
#print(top)
#print()
text_headlines = top.text
print(text_headlines)
print()
n +=1
顺便说一句,我应该在 views.py 和模板中添加什么。谢谢;)
【问题讨论】:
-
你把它放在views.py中。
标签: python django beautifulsoup request