【问题标题】:Parsing the html page with images用图像解析 html 页面
【发布时间】:2019-09-10 09:47:19
【问题描述】:

在我提供的那个视频中,这个人正在解析 Google 图片。

https://www.youtube.com/watch?v=MaWm1VpWj1A&list=LLiAY9i8Qha2Vx2j21Jk88PA&index=2&t=0s

import requests
from bs4 import BeautifulSoup as bs
import random
import json
import lxml.html

ua = "Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36                     
(KHTML, как Gecko) Chrome / 58.0.3029.110 Safari / 537.36"

headers = {"User-Agent": random.choice(ua)}

r = requests.get("https://www.google.ru/search?tbm=isch&q=яблоко",                 
headers=headers)

html = r.content

soup = bs(html, "lxml")

images = soup.find_all("div", {"class": "rg_meta notranslate"})

images = [i.text for i in images]
images = [json.loads(i) for i in images]

print(images)

但我什么都没有

[]

进程以退出代码 0 结束

soup - 显示 html 文本,但 images = soup.find_all("div", {"class": "rg_meta notranslate"}) - 不显示任何内容
我已经查看了很多文章,我可以在任何地方找到一些东西并使用find_all 输出它 - 我什么也做不了,为什么,我不知道。 请帮助我了解发生了什么

【问题讨论】:

  • soup.find_all("img") 有用吗?

标签: python-3.x beautifulsoup request


【解决方案1】:

要以requests 的方式查看页面,您必须关闭JavascriptNoScript 是一个可以执行此操作的浏览器扩展程序)。

然后要抓取图像,您必须在 html 中识别它们:

正如您在此处看到的,没有类 "rg_meta""no_translate" 的元素,这就是您什么都得不到的原因。

正如 cmets 中所建议的,您可以使用:

soup.find_all('img')

或者,您可以使用“images_table”类获取表格,获取其'tbody' 元素,然后迭代每个'tr''td' 元素以获取图像。

【讨论】:

  • 感谢您快速而清晰的回答,但是我有一个新问题 - 如何获得完整的图像质量?
猜你喜欢
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
相关资源
最近更新 更多