Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美化组:提取img alt数据_Python_Html_Beautifulsoup_Scrape - Fatal编程技术网

Python 美化组:提取img alt数据

Python 美化组:提取img alt数据,python,html,beautifulsoup,scrape,Python,Html,Beautifulsoup,Scrape,我有下面的图像html,我正在尝试解析alt中的信息。目前我能够成功地提取图像 html(我当前解析的内容) <img class="rslp-p" alt="Sony Cyber-shot DSC-W570 16.1 MP Digital Camera - Silver" src="http://i.ebayimg.com/00/$(KGrHqZ,!j!E5dyh0jTpBO(3yE7Wg!~~_26.JPG?set_id=89040003C1" itemprop="image" /&g

我有下面的图像html,我正在尝试解析alt中的信息。目前我能够成功地提取图像

html(我当前解析的内容)

<img class="rslp-p" alt="Sony Cyber-shot DSC-W570 16.1 MP Digital Camera - Silver" src="http://i.ebayimg.com/00/$(KGrHqZ,!j!E5dyh0jTpBO(3yE7Wg!~~_26.JPG?set_id=89040003C1" itemprop="image" />
我想做的是提取

alt="Sony Cyber-shot DSC-W570 16.1 MP Digital Camera - Silver"

我还想在提取图像时使用alt数据作为文件名。

for
循环中,您只需执行以下操作即可获得该文件名

image.get('alt', '')

这在(“标记的属性”)中进行了解释。

您正在使用
image['src']
获取源代码。您不能只使用
image['alt']
获取alt,还是我误解了你的问题?关键错误意味着特定的img标记没有alt属性。你确定页面上的每个图像都有与之关联的alt文本吗?编辑后的答案,它应该适用于@anyamaired提及的情况。事实上,一些没有,我试图避免那些没有have@GonzaloDelga非常感谢如何将alt信息添加为文件名..?取决于您希望文件名的外观,您可以将其混合到示例代码的文件名结构中,尽管有很大的改进空间,但我建议您在代码审阅时询问这一点
image.get('alt', '')