我想提取维基百科上"谷歌"页面的所有完整图像URL
我尝试过:
http://en.wikipedia.org/w/api.php?action=query&titles=Google&generator=images&gimlimit=10&prop=imageinfo&iiprop=url|dimensions|mime&format=json
但是,通过这种方式,我也没有得到谷歌相关的图像,例如:
http://upload.wikimedia.org/wikipedia/en/a/a4/Flag_of_the_United_States.svg
http://upload.wikimedia.org/wikipedia/en/4/4a/Commons-logo.svg
http://upload.wikimedia.org/wikipedia/en/4/4a/Commons-logo.svg
http://upload.wikimedia.org/wikipedia/commons/f/fe/Crystal_Clear_app_browser.png
如何只提取我在谷歌页面上看到的图像
- 检索页面源代码,https://en.wikipedia.org/w/index.php?title=Google&动作=原始
- 扫描它以查找类似
[[File:Google web search.png|thumb|left|On February 14, 2012, Google updated its homepage with a minor twist. There are no red lines above the options in the black bar, and there is a tab space before the "+You". The sign-in button has also changed, it is no longer in the black bar, instead under it as a button.]]
的子字符串 - 向API索取页面上的所有图片,http://en.wikipedia.org/w/api.php?action=query&title=谷歌&generator=图像&gimlimit=10&prop=imageinfo&iiprop=url|dimensions|mime&format=json
- 过滤掉url,但那些与步骤2中找到的图片名称匹配的url除外
步骤2和4需要更多解释。
@2.Regexp/b(File|Image):[^]|nr]+/
应该足够了。在Ruby的regexp中,b
表示单词边界,这在您选择的语言中可能不受支持。我提出的Regexp将匹配我脑海中的所有案例:[[File:something.jpg]]
,图库标签:<gallery>nFile:one.jpgnFile:two.jpgn</gallery>
,模板:{{Infobox|pic = File:something.jpg}}
。但是,它与包含]
的文件名不匹配。我不确定它们是否合法,但如果是,它们一定非常罕见,这应该不是什么大不了的事。
如果您只想匹配这样的构造:[[File:something.jpg|thumb|description]]
,下面的regexp会更好地工作:/[[(File|Image):[^]|]+/
@4.我会删除与/[^A-Za-z0-9]/
匹配的名称中的所有字符。这比逃离它们更容易,而且在大多数情况下,这已经足够了。
图标最常附加在模板中,而与文章主题相关的图片最常直接附加([[File:…]]
)。不过也有例外,例如在一些文章中,图片是用{{Gallery}}模板附加的。还有<gallery>
标记,它为库引入了特殊的语法。你必须根据你的需求调整我的解决方案,即使这样也不会完美,但它应该足够好。