javascript - 我需要计算网页中具有特定值 "src"的图像并填充输入字段并使用脚本自动提交页面

标签 javascript python web-scraping vbscript scripting

假设任何带有表单并具有一组图像的网页。我需要在页面加载和提交时填充输入文本值。

var imgs = document.getElementsByTagName('img');
var countImagesFilled = 0;

var curImg;
for(var i = 0; i < imgs .length; i++) {
  curImg = imgs[i];
if(curImg.getAttribute('src')=='abc.png'){++countImagesFilled;}
}
var inputValue = document.getElementsByName('valuee');
inputValue[0].value=countImagesFilled;
document.forms[0].submit();

最佳答案

看看这个 working example :)

window.onload = ()=>{
  // get our form
  let imageForm = document.getElementById('image-form');
  // something to inform us that the form has been submitted
  imageForm.addEventListener('submit', function(){
    console.log(`form data: ${$(this).serialize()}`);
  });
  
  // get all the img elements
  let images = document.querySelectorAll('img');
  // get the input field where we'll be putting the total image count
  let field = document.getElementById('image-count');
  // the number of images that match our criteria
  let imageCount = 0;
  // count the number of img elements that have 'test.png' as their source
  for(let i = 0; i < images.length; i++){
    if(images[i].getAttribute('src') === 'test.png'){
      imageCount++;
    }
  }
  
  // set the image count (imageCount) as the value of the text field
  field.value = imageCount;
  
  // submit the form
  imageForm.dispatchEvent(new Event('submit'));
};
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<form id="image-form" name="imageCount">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="test.png" class="img-1">
  <img src="exclude.png" class="img-2">
  <img src="exclude.png" class="img-2">
  <img src="exclude.png" class="img-2">
  <img src="exclude.png" class="img-2">
  <img src="exclude.png" class="img-2">
  <input type="text" id="image-count" name="imageWithSourceCount">
</form>

还有一件事,考虑到以下 HTML:

<img src="images/test.png" class="img-1">

如果您读取图像对象 (img.src) 的 src 属性,您将获得解析路径,类似于 yourwebsite.com/images/test.png。但是,如果您使用 getAttribute (img.getAttribute('src')) 检索它,您将获得 src 属性的实际值这也可能是相对路径(即 images/test.png)

关于javascript - 我需要计算网页中具有特定值 "src"的图像并填充输入字段并使用脚本自动提交页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52798964/

相关文章:

python - antlr4 python目标无法识别unicode

excel - 使用 VBA 在 span 标签内的 <p> 元素内获取文本

html - 使用css选择器excel vba从网站抓取数据

javascript - 在javascript中的某个位置找到单词

javascript - 如何使用 Elastic Beanstalk 创建 "tmp"目录?

javascript - 如何从具有html代码的内容中仅获取<li>标签innerHTML

Python添加到MySQL数据库

javascript - 在可编辑的 div 中可靠地获取和设置光标/插入符位置

java - Java 中的 Python 3 PBKDF2HMACBackend default_backend() 等效项

python - 设置 verify_certs=False yet elasticsearch.Elasticsearch throws SSL error for certificate verify failed