gpt4 book ai didi

javascript - 将 Javascript 生成的表上的 值抓取到 Python

转载 作者:太空狗 更新时间:2023-10-29 15:15:50 25 4
gpt4 key购买 nike

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

@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'))

如您所见,我从我的表单中获取每个值,并使用 beautifulsoup 抓取我表中的值。但是,在我单击提交按钮后,它会永远加载。我从上部 Pane 但不在表中获取 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>

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>

这些爬取值的数据,我希望它们能保存到我的数据库中。在一个单元格上。如果可能的话,我希望将列表插入到一个列中,以便稍后获取它们。

或者有什么方法可以让我的数据库更干净、更好地获取列表?任何帮助表示赞赏。谢谢!

最佳答案

所以看起来你正在使用请求来尝试获取 JS 生成的数据。嗯,这是行不通的,除非你知道一些很多人不知道的魔法。请求无法处理 JS,因此它永远不会运行。您应该能够使用 selenium 或其他自动化浏览器来获取数据。否则,我认为您将无法像这样刮掉它。但是,如果有人知道一种通过请求获取 JS 生成数据的方法,请发布。

关于javascript - 将 Javascript 生成的表上的 <td> 值抓取到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47836276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com