简体   繁体   English

在我的网站中使用我的Python Web爬网程序

[英]Using my Python Web Crawler in my site

I created a Web Crawler in Python 3.7 that pulls different info and stores them into 4 different arrays. 我在Python 3.7中创建了一个Web爬网程序,该爬网程序提取不同的信息并将其存储到4个不同的数组中。 I have now come across an issue that I am not sure how to fix. 我现在遇到了一个不确定如何解决的问题。 I want to use the data from those four arrays in my site and place them into a table made from JS and HTML/CSS. 我想使用站点中这四个数组中的数据,并将它们放入由JS和HTML / CSS制成的表中。 How do I go about accessing the info from my Python file in my JavaScript file? 如何从JavaScript文件中的Python文件访问信息? I tried searching in other places before creating an account, and came across some things that talk of using Json, but I am not too familiar with these and would appreciate some help if that is the way to do it. 在创建帐户之前,我曾尝试在其他地方进行搜索,并且遇到了一些有关使用Json的事情,但是我对这些事情不太熟悉,如果这样做的话,我将不胜感激。 I will post my code below which I have stored in the same directory as my other sites files. 我将在下面发布我的代码,并将其存储在与其他站点文件相同的目录中。 Thanks in advance! 提前致谢!

from requests import get
from bs4 import BeautifulSoup
from flask import Flask
app = Flask(__name__)


@app.route("/")
def main():
    # lists to store data
    names = []
    gp = []
    collectionScore = []
    arenaRank = []

    url = 'https://swgoh.gg/g/21284/gid-1-800-druidia/'
    response = get(url)

    soup = BeautifulSoup(response.content, 'html.parser')

    # username of the guild members:
    for users in soup.findAll('strong'):
        if users.text.strip().encode("utf-8") != '':
            if users.text.strip().encode("utf-8") == '\xe9\x82\x93\xe6\xb5\xb7':
                names.append('Deniz')
            else:
                names.append(users.text.strip().encode("utf-8"))
        if users.text.strip().encode("utf-8") == 'Note':
            names.remove('Note')
        if users.text.strip().encode("utf-8") == 'GP':
            names.remove('GP')
        if users.text.strip().encode("utf-8") == 'CS':
            names.remove('CS')

    print(names)

    # GP of the guild members:
    for galacticPower in soup.find_all('td', class_='text-center'):
        gp.append(galacticPower.text.strip().encode("utf-8"))
    totLen = len(gp)

    i = 0
    finGP = []
    while i < totLen:
        finGP.append(gp[i])
        i += 4
    print(finGP)

    # CS of the guild members:
    j = 1
    while j < totLen:
        collectionScore.append(gp[j])
        j += 4
    print(collectionScore)

    # Arena rank of guild member:
    k = 2
    while k < totLen:
        arenaRank.append(gp[k])
        k += 4
    print(arenaRank)

if __name__ == "__main__":
    app.run()

TLDR: I want to use the four lists - finGP, names, collectionScore, and arenaRank in a JavaScript or HTML file. TLDR:我想在JavaScript或HTML文件中使用四个列表-finGP,名称,collectionScore和arenaRank。 How do I go about doing this? 我该怎么做呢?

Ok, this will be somewhat long but I'm going to try breaking it down into simple steps. 好的,这会有些长,但是我将尝试将其分解为简单的步骤。 The goal of this answer is to: 该答案的目的是:

  1. Have you get a basic webpage being generated and served from python. 您是否已从python生成并提供了一个基本网页。
  2. Insert the results of your script as javascript into the page. 将脚本的结果作为javascript插入页面。
  3. Do some basic rendering with the data. 用数据做一些基本的渲染。

What this answer is not: 这个答案不是:

  1. An in-depth javascript and python tutorial. 深入的javascript和python教程。 We don't want to overload you with too many concepts at one time. 我们不想一次给您太多概念。 You should eventually learn about databases and caching, but that's further down the road. 您最终应该了解数据库和缓存,但是这还很远。

Ok, here's what I want you to do first. 好的,这是我首先要您执行的操作。 Read and implement this tutorial up until the "Creating a Signup Page" section. 阅读并实施本教程 ,直到“创建注册页面”部分。 That starts to get into dealing with Mysql, which isn't something you need to worry about right now. 这开始涉及到Mysql的处理,您现在不必担心这一点。

Next, you need to execute your scraping script when a request for the server. 接下来,您需要在请求服务器时执行抓取脚本。 When you get the results back, you output those into the html page template inside a script tag that looks like: 返回结果后,将其输出到脚本标签中的html页面模板中,该脚本标签如下所示:

<script>
  const data = [];
  console.log(data);
</script>

Inside the brackets in data = [] use json.dumps ( https://docs.python.org/2/library/json.html ) to format your Python array data as json. data = []的方括号内,使用json.dumpshttps://docs.python.org/2/library/json.html )将Python数组数据格式化为json。 Json is actually a subset of javascript, so you just output it as a raw javascript string here and it gets loaded into the webpage via the script tag. Json实际上是javascript的子集,因此您只需在此处将其输出为原始javascript字符串,然后就可以通过script标签将其加载到网页中。

The console.log statement in the script tag will show the data in the dev tools in your browser. 脚本标记中的console.log语句将在浏览器的dev工具中显示数据。

For now, lets pause here. 现在,让我们在这里暂停。 Get all of this working first (probably a few hours to a day's work). 首先完成所有工作(一天的工作可能需要几个小时)。 Getting into doing html rendering with javascript is a different topic and again, I don't want to overload you with too much information right now. 开始使用javascript进行html渲染是一个不同的话题,我不想现在就给您过多的信息。

Leave comments on this answer if you need extra help. 如果您需要其他帮助,请在此答案上留下评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM