首页 > 解决方案 > 为什么抓取检测只有 20 个 img 标签?

问题描述

from urllib.request import Request,urlopen,urlretrieve
import urllib
from bs4 import BeautifulSoup
key = input("Enter the image to be searched \n")
urlpath = "https://www.google.com/search? 
   q="+key+"&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiE9- 
   7LjobfAhXCEHIKHQy6A00Q_AUIDigB&biw=1920&bih=947#imgrc=_"
page_req = Request(urlpath, headers={'User-Agent': 'Mozilla/5.0'})
page= urlopen(page_req).read()
soup =BeautifulSoup(page,'html.parser')
images= soup.find_all('img')
print("Total"+str(len(images)))
image_links=[]
for img in images:
    image_links.append(img.get('src'))
image_count=0
for link in image_links:
    urlretrieve(link,'image_'+str(image_count)+'.jpg')
    image_count+=1

上面的脚本只从谷歌图片页面检测到 20 个图片标签。为什么它无法检测到网页中的所有图像标签?

标签: python-3.xweb-scrapingbeautifulsoup

解决方案


该 URL 处的 HTML 文档仅包含 20 张图像。

其余部分在页面加载后通过使用 JavaScript 修改 DOM 来加载。


推荐阅读