簡體   English   中英

使用 python 上的 requests_html 解析 img src url

[英]Parse an img src url using requests_html on python

我正在嘗試使用 HTML ZA2F21ED4F8EBC2CBB14 參考 Python 獲取 img src URL

我的代碼現在看起來像這樣,這些是我一直在使用的庫:

from requests_html import HTMLSession
import concurrent.futures
import sys
import sqlite3
data = {'url': url}
resp = sess.get(url)

artwork = resp.html.find('.single-album-tombstone__art img')
if artwork:
   data['artwork'] = artwork[0].text

這不起作用,它只返回一個“EMPTY”字符串。 如何獲取圖像 URL? 我試過使用.attrs['src']但也沒有成功。

你可以像這樣從img標簽解析src屬性

from requests_html import HTMLSession
from bs4 import BeautifulSoup

url = "https://www.google.com/search?q=james+bond&safe=active&tbm=isch"

with HTMLSession() as session:
    response = session.get(url)
    response.html.render()
    content = response.html.html

soup = BeautifulSoup(content, "html.parser")
images = soup.find_all("img")
for img in images:
    src = img.get("src")
    if src:
        print(src)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM