- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对数据抓取不太熟悉,并且无法使用 beautiful soup 下载图像。
我需要从网站下载所有图像。我正在使用下面的代码:
import re
import requests
from bs4 import BeautifulSoup
site = 'http://someurl.org/'
response = requests.get(site)
soup = BeautifulSoup(response.text, 'html.parser')
# img_tags = soup.findAll('img')
img_tags = soup.findAll('img',{"src":True})
print('img_tags: ')
print(img_tags)
urls = [img['src'] for img in img_tags]
print('urls: ')
print(urls)
for url in urls:
filename = re.search(r'/([\w_-]+[.](jpg|gif|png))$', url)
with open(filename.group(1), 'wb') as f:
if 'http' not in url:
# sometimes an image source can be relative
# if it is provide the base url which also happens
# to be the site variable atm.
url = '{}{}'.format(site, url)
response = requests.get(url)
f.write(response.content)
但是,这会忽略页面上存在的所有具有与此类似的 html 的图像:
<img data-bind="attr: { src: thumbURL() }" src="/assets/images/submissions/abfc-2345345234.thumb.png">
我认为这是因为数据属性也包含字符串“src”,但我似乎无法弄清楚。
最佳答案
你需要使用selenium或者一些可以运行javascript的东西。这是代码加载图像直到找到它
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
site = 'http://phylopic.org/'
dr = webdriver.Chrome()
dr.get(site)
try:
element = WebDriverWait(dr, 20, 0.5).until(
EC.visibility_of_element_located((By.CLASS_NAME, "span1"))
)
except:
print("Wait a bit more")
time.sleep(5)
text = dr.page_source
soup = BeautifulSoup(text,"lxml")
imgs = soup.find_all('img')
print(imgs)
dr.close()
第二个问题是如何将相对路径转换为绝对路径。 relative path 有几种类型上HTML
.
当网址为 http://someurl.org/somefd/somefd2
时
<img src="picture.jpg"> http://someurl.org/somefd/somefd2/picture.jpg
<img src="images/picture.jpg"> http://someurl.org/somefd/somefd2/images/picture.jpg
<img src="/images/picture.jpg"> http://someurl.org/images/picture.jpg
<img src="../picture.jpg"> http://someurl.org/somefd/picture.jpg
这是我将 rp 转换为 ap 的代码。
import re
site = 'https://en.wikipedia.org/wiki/IMAGE'
def r2a(path,site=site):
rp = re.findall(r"(/?\W{2}\/)+?",path)
if path.find("http") == 0:
#full http url
return path
elif path.find("//") == 0:
#http url lack of http:
return "http:" + path
elif path.find("//") < 0 and path.find("/") == 0:
# located in the folder at the root of the current web
site_root = re.findall("http.{3,4}[^/]+",site)
return site_root[0] + path
elif rp:
# located in the folder one level up from the current folder
sitep = len(re.findall(r"([^/]+)+",site)) - 2 - len(rp)
# raise error when sitep-len(rp)
new_path = re.findall("(http.{4}[^/]+)(/[^/]+){%d}"%(sitep),site)
return "{}/{}".format("".join(new_path[0]),path.replace( "".join(rp) , ""))
else:
# located in the folder one level up from the current folder
# located in the same folder as the current page
return "{}/{}".format(site,path)
assert "https://en.wikipedia.org/wiki/IMAGE/a.jpg" == r2a("a.jpg")
assert "https://en.wikipedia.org/wiki/IMAGE/unknow/a.jpg" == r2a("unknow/a.jpg")
assert "https://en.wikipedia.org/unknow/a.jpg" == r2a("/unknow/a.jpg")
assert "https://en.wikipedia.org/wiki/a.jpg" == r2a("../a.jpg")
assert "https://en.wikipedia.org/a.jpg" == r2a("../../a.jpg")
assert "https://en.wikipedia.org/wiki/IMAGE/a.jpg" == r2a("https://en.wikipedia.org/wiki/IMAGE/a.jpg")
assert "http://en.wikipedia.org/" == r2a("//en.wikipedia.org/")
关于python - 无法使用 Beautiful Soup 下载所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051407/
我不知道“汤”字面意思在与计算机图形相关的“三角形汤”或“多边形汤”中使用时是什么意思。是不是和我们用勺子吃饭的“汤”有关? (我的母语不是英语。) 最佳答案 维基百科来拯救! A polygon s
我们正在废弃 Amazon.in 网站以检索任何产品的价格。所有产品在“span”标签中的“id”属性都具有不同的值,例如; id = 'priceblock_ourprice', id = 'p
我有一个这样的模板: 和这样的输入 HTML COMPLEX HTML 其中 COMPLEX_HTML 是很多子标签(很干净 - 验证) 我试图将输入 HTML 的 body 标记内的 HTML
我对 soup('tag_name') 和 soup.find_all('tag_name') 之间的区别感到困惑。下面是一个包含一小段 html 的示例: from bs4 import Beaut
我正在尝试使用 css 选择器解析 html 页面 import requests import webbrowser from bs4 import BeautifulSoup page = req
这是网页 HTML 源代码的一部分: apple banana cherry melon 我想提取我想要的网址,比如以/Result 开头的网址?我刚刚了解到您可以在 beautiful soup
我注意到一个非常烦人的错误:BeautifulSoup4(包:bs4)经常发现比以前版本(包:BeautifulSoup)更少的标签。 这是该问题的一个可重现的实例: import requests
所以我一直在试图弄清楚如何抓取一个购买/销售网站的网站,我发现了 HTML 中的所有内容,但该类包含不同的随机数,例如:
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwa
我正在尝试加载 html 页面并输出文本,即使我正确获取网页,BeautifulSoup 以某种方式破坏了编码。 来源: # -*- coding: utf-8 -*- import requests
题目地址:https://leetcode.com/problems/soup-servings/description/ 题目描述: There are two types of soup: t
您好,我正在尝试从网站获取一些信息。请原谅我,如果我的格式有任何错误,这是我第一次发布到 SO。 soup.find('div', {"class":"stars"}) 从这里我收到 我需要 “
我想从 Google Arts & Culture 检索信息使用 BeautifulSoup。我检查了许多 stackoverflow 帖子( [1] , [2] , [3] , [4] , [5]
我决定学习 Python,因为我现在有更多时间(由于大流行)并且一直在自学 Python。 我试图从一个网站上刮取税率,几乎可以获得我需要的一切。下面是来自我的 Soup 变量以及相关 Python
我正在使用 beautifulsoup 从页面中获取所有链接。我的代码是: import requests from bs4 import BeautifulSoup url = 'http://ww
我正在尝试根据部分属性值来识别 html 文档中的标签。 例如,如果我有一个 Beautifulsoup 对象: import bs4 as BeautifulSoup r = requests.ge
Показать телефон 如何在 Beautiful Soup 中找到上述元素? 我尝试了以下方法,但没有奏效: show = soup.find('div', {'class': 'acti
我如何获得结果网址:https://www.sec.gov/Archives/edgar/data/1633917/000163391718000094/0001633917-18-000094-in
我是 python 新手,尝试从页面中提取表格,但无法使用 BS4 找到该表格。你能告诉我我哪里出错了吗? import requests from bs4 import BeautifulSoup
我有一个巨大的 XML 文件(1.2 G),其中包含数百万个 MusicAlbums 的信息,每个 MusicAlbums 都具有如下简单格式 P 22 Exitos
我是一名优秀的程序员,十分优秀!