views.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:ptt-web-crawler 作者: ChinHui-Chen 项目源码 文件源码
def home(request):
    if request.method == 'GET':
        return render(
            request,
            'demo/demo.html',
        )
    elif request.method == 'POST' and request.is_ajax():
        bname = escape(request.POST.get('board_name'))
        aid = escape(request.POST.get('article_id'))
        link = PTT_URL + '/bbs/' + bname + '/' + aid + '.html'
        if not (bname and aid):
            return HttpResponse(json.dumps({'data': {'error': 'invalid url'}, 'link': link}), content_type='application/json')
        if aid.lower() == 'latest' or aid.lower() == 'index':
            resp = requests.get(
                url=PTT_URL + '/bbs/' + bname + '/index.html',
                cookies={'over18': '1'}, verify=False
            )
            if resp.status_code == 200:
                soup = BeautifulSoup(resp.text)
                divs = soup.find_all("div", "r-ent")
                aid = divs[-1].select("div.title > a")[0]['href'].split("/")[3].replace(".html", "")
        link = PTT_URL + '/bbs/' + bname + '/' + aid + '.html'
        data = json.loads(
            crawler.parse(link, aid, bname)
        )
        return HttpResponse(json.dumps({'data': data, 'link': link}), content_type='application/json')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号