在表上刮取<td>值由Javascript生成到Python

Bri*_*rix 6 html javascript python web-scraping

我的网络应用程序出现了问题.这是我的代码:

@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():

   if g.user:
        if request.method == 'POST':

#UPPER PANE

            payor = request.form['payor']


            receiptno = request.form['OR']
            paymentmethod = request.form['paymentmethod']
            naive_dt = time.strftime("%m/%d/%Y")
            collectiondate = naive_dt = datetime.now() 
            message = request.form['message']
#LOWER PANE
            url_to_scrape = 'http://localhost:5000/form'
            r = requests.get(url_to_scrape)
            soup = BeautifulSoup(r.text)
            nature = []
            for table_row in soup.select("table.inmatesList tr"):
              cells = table_row.findAll('td')
              if len(cells) > 0:
                nature = cells[0].text.strip()
                natureamt = cells[1].text.strip()
                nature = {'nature': nature, 'nature': natureamt}
                nature_list.append(nature)
            ent = Entry(receiptno, payor,officer, paymentmethod, collectiondate,message, nature_list)
            add_entry(ent)
            actions="Applied"

            return redirect(url_for('form'))

   return redirect(url_for('home'))
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我从表单中获取每个值,并使用beautifulsoup来抓取表中的值.但是,在我单击提交按钮后,它将永远加载.我从上部窗格获得了valeus,但没有在表格中获得.

顺便说一句,我从一个javascript函数onClick生成我的单元格.以防我的javascript可能是问题.或者可能有一种从javascrip函数 - > python中提取这些值的简单方法.这是我的javascript代码和HTML

<script type="text/javascript">
    function deleteRow(o){
     var p=o.parentNode.parentNode;
         p.parentNode.removeChild(p);
      }

     function addRow()
      {

        var table = document.getElementById("datatable"),
          newRow = table.insertRow(table.length),
          cell1 = newRow.insertCell(0),
          cell2 = newRow.insertCell(1),
          cell3 = newRow.insertCell(2),


          name = document.getElementById("form").value,
          amount = document.getElementById("amount").value;


          delete1 = delete1 = '<input type="button"  class="btn btn-danger" class="glyphicon glyphicon-trash"id="delete" value="Delete" onclick="deleteRow(this)">';
        cell1.innerHTML = name;
        cell2.innerHTML = amount;
        cell3.innerHTML = delete1;

        findTotal();
      }


 function findTotal(){
   var arr = document.querySelectorAll("#datatable td:nth-child(2)");
   var tot=0;

   for(var i=0;i<arr.length;i++){
      var enter_value   = Number(arr[i].textContent)
      if(enter_value)
                tot += Number(arr[i].textContent);
      }
   document.getElementById('total').value = tot;
 }

</script>
Run Code Online (Sandbox Code Playgroud)

HTML:

                    <form name="noc">  

                      <input class="form-control input-lg" id="form" list="languages" placeholder="Search" type="text" required>
                      <br>
                      <input class="form-control input-lg" id="amount" list="languages" placeholder="Amount" type="number" required>
                      <br>
                      <button onclick="addRow(); return false;">Add Item</button>
                    </form>




      <table id="datatable" class="table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
    <tr>
    <th>Nature of Collection</th>
    <th>Amount</th>
    <th></th>

    </tr>
        </thead>
<tbody>
<tr>

        </tr>     
</tbody>

</table>
Run Code Online (Sandbox Code Playgroud)

这些刮取值的数据,我希望它们保存到我的数据库中.在一个细胞上.如果可能的话,我希望将列表插入列中,以便稍后再获取.

或者有没有办法让我更清晰,更好地获取数据库的列表?任何帮助表示赞赏.谢谢!

Sup*_*tew 1

所以看起来您正在使用请求来尝试获取 JS 生成的数据。好吧,这行不通,除非你知道一些很多人不知道的魔法。请求无法处理 JS,因此它永远不会运行。您应该能够使用 selenium 或其他自动化浏览器来获取数据。不然的话,我想你不可能这么刮的。但如果有人知道如何通过请求获取 JS 生成的数据,请发布。