Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python爬网中通过调用JavaScript函数保存选择_Javascript_Python_Beautifulsoup_Web Crawler_Scraper - Fatal编程技术网

如何在Python爬网中通过调用JavaScript函数保存选择

如何在Python爬网中通过调用JavaScript函数保存选择,javascript,python,beautifulsoup,web-crawler,scraper,Javascript,Python,Beautifulsoup,Web Crawler,Scraper,登录后,我可以进入这个页面,这里是它的HTML、JS和Python代码。我想做多项选择,然后,我想使用HTML底部的保存按钮(input type=“submit”onclick=“saveVariations()”)保存选择 <body> <tbody> <tr> <th scope="row">abc</th> <td>1</td>

登录后,我可以进入这个页面,这里是它的HTML、JS和Python代码。我想做多项选择,然后,我想使用HTML底部的保存按钮(input type=“submit”onclick=“saveVariations()”)保存选择

    <body>        
    <tbody>

    <tr>
      <th scope="row">abc</th>
      <td>1</td>
      <td>1.0</td>
      <td><input type="checkbox" value="abc" onclick="addToVariations(this);"></td>
    </tr>

    <tr>
      <th scope="row">xyz</th>
      <td>1</td>
      <td>1.0</td>
      <td><input type="checkbox" value="xyz" onclick="addToVariations(this);"></td>
    </tr>
    .
    .
    .
    .
    </tbody>
  </table>
 <input type="button" value="Save variations" onclick="saveVariations()"/>
</body>
下面是Javascript代码

  <script>
     var selectedVariations = [];
     function addToVariations(cb) {
         if (selectedVariations.indexOf(cb.value) > -1) {
             selectedVariations.splice(selectedVariations.indexOf(cb.value), 1);
         }
         else {
             selectedVariations.push(cb.value);
         }

         console.log(selectedVariations);
     }

function saveVariations() {
    if (selectedVariations.length > 0) {
        console.log(selectedVariations);
        $.ajax({
            type: "POST"
            , url: "/dishes/-K9uHK4M-pu9p9yJGl9a/variations"
            , data: JSON.stringify(selectedVariations)
            , contentType: 'application/json;charset=UTF-8'
            , success: function (result) {
                console.log(result.Status);
                if (result.Status == 200) {
                    $.notify("Succesfully saved variations!", "success");
                    window.location.href = "/";
                }
                else {
                    $.notify("Something went wrong!", "error");
                }

            }
        });
    }
}

var-selectedVariations=[];
函数addToVariations(cb){
如果(已选择变体索引of(cb.value)>-1){
selectedVariations.splice(selectedVariations.indexOf(cb.value),1);
}
否则{
所选变量推送(cb值);
}
console.log(选择的变体);
}
函数saveVariations(){
如果(selectedVariations.length>0){
console.log(选择的变体);
$.ajax({
类型:“职位”
,url:“/disks/-K9uHK4M-pu9p9yJGl9a/变体”
,数据:JSON.stringify(selectedVariations)
,contentType:'application/json;charset=UTF-8'
,成功:功能(结果){
控制台日志(结果状态);
如果(result.Status==200){
$.notify(“成功保存变体!”,“成功”);
window.location.href=“/”;
}
否则{
$.notify(“出错了!”,“错误”);
}
}
});
}
}

我无法重现这个问题。你的代码对我有用,但给了我第一个
,但我把它改为
查找所有('input')[2]
,得到了
你能告诉我如何选择并保存任何选项吗?这是javascript-BeautifulSoup无法执行javascript。您可能想退房,或者。看到这个答案:selenium很慢,我尝试了另一个项目。每次它打开浏览窗口,然后显示输出。有没有一种方法可以在不使用selenium打开浏览器的情况下做到这一点。可能不使用selenium,但可能可以做到,请参见以下答案:。这是高度投票和接受,所以也许它可以。
  <script>
     var selectedVariations = [];
     function addToVariations(cb) {
         if (selectedVariations.indexOf(cb.value) > -1) {
             selectedVariations.splice(selectedVariations.indexOf(cb.value), 1);
         }
         else {
             selectedVariations.push(cb.value);
         }

         console.log(selectedVariations);
     }

function saveVariations() {
    if (selectedVariations.length > 0) {
        console.log(selectedVariations);
        $.ajax({
            type: "POST"
            , url: "/dishes/-K9uHK4M-pu9p9yJGl9a/variations"
            , data: JSON.stringify(selectedVariations)
            , contentType: 'application/json;charset=UTF-8'
            , success: function (result) {
                console.log(result.Status);
                if (result.Status == 200) {
                    $.notify("Succesfully saved variations!", "success");
                    window.location.href = "/";
                }
                else {
                    $.notify("Something went wrong!", "error");
                }

            }
        });
    }
}