Python 中的正则表达式问题
Troubles with regex in Python
我正在编写一个简短的 Python 脚本,它可以在 phpbb 论坛数据库中找到指向 Photobucket 中托管的图片的所有 URL,并将它们传递给下载管理器(在我的例子中是免费下载管理器)为了将图像保存在本地计算机中,然后将它们移动到另一台主机上(现在 Photobucket 开始要求按年订阅以将托管在其服务器中的图片嵌入到其他站点中)。当我在两个支持正则表达式搜索的文本编辑器上测试我的正则表达式时,我已经设法使用带有环视的正则表达式搜索了所有图片,我找到了我想要的东西,但在我的脚本中它给我带来了麻烦。
import re
import os
main_path = input("Enter a path to the input file:")
with open(main_path, 'r', encoding="utf8") as file:
file_cont = file.read()
pattern = re.compile(r'(?!(<IMG src=""))http:\/\/i[0-9][0-9][0-9]\.photobucket\.com\/albums\/[^\/]*\/[^\/]*\/[^\/]*(?=("">))')
findings = pattern.findall(file_cont)
for finding in findings:
print(finding)
os.system("pause")
我尝试调试它删除下载部分并打印所有匹配项,我得到一长串 (''
, '"">'
) 而不是类似于此的 URL:http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg
我哪里错了?
你的正则表达式模式不好。
我不确定你想做什么,我建议你使用 BeautifulSoup instead of playing with regex if you needs to parse HTML (because Regex can not really parse HTML).
但无论如何 - 使用正则表达式 - 这应该有效:
r'<IMG src=\"(https?:\/\/i[0-9]{3}\.photobucket\.com\/albums[^\"]+)\"[^>]+\/>'
https?:\/\/i[0-9]{3}\.photobucket\.com\/albums
用于过滤非 photobucket 图像,[^\"]+
更通用,只提取属性的最后一个 "
字符之前的所有内容。
示例:
<IMG src="http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg" foo="bar"/>
在 .group(1)
给予:
http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg
我认为您的正则表达式的以下版本应该可以工作:
请注意,我使用 \"
而不是 ""
,
我将 img src
替换为 img.+src
以支持 img alt="" src
,
而不是 [^\/]*
我使用 [^\/]+
来删除 \
,
的验证
对于 URL 的最后一部分,我还检查是否没有出现 "
、
然后,我没有检查紧跟在 "
之后的 >
,而是通过 .*
.
检查 "
之后的可选其他字符
(?!(<img.+src=\"))http:\/\/i\d{3}\.photobucket\.com\/albums\/[^\/]+\/[^\/]+\/[^\/\"]+(?=\".*/>)
^^ ^^^
您可以使用 \d\d\d
或 [0-9]{3}
或 \d{3}
代替 [0-9][0-9][0-9]
,
我正在编写一个简短的 Python 脚本,它可以在 phpbb 论坛数据库中找到指向 Photobucket 中托管的图片的所有 URL,并将它们传递给下载管理器(在我的例子中是免费下载管理器)为了将图像保存在本地计算机中,然后将它们移动到另一台主机上(现在 Photobucket 开始要求按年订阅以将托管在其服务器中的图片嵌入到其他站点中)。当我在两个支持正则表达式搜索的文本编辑器上测试我的正则表达式时,我已经设法使用带有环视的正则表达式搜索了所有图片,我找到了我想要的东西,但在我的脚本中它给我带来了麻烦。
import re
import os
main_path = input("Enter a path to the input file:")
with open(main_path, 'r', encoding="utf8") as file:
file_cont = file.read()
pattern = re.compile(r'(?!(<IMG src=""))http:\/\/i[0-9][0-9][0-9]\.photobucket\.com\/albums\/[^\/]*\/[^\/]*\/[^\/]*(?=("">))')
findings = pattern.findall(file_cont)
for finding in findings:
print(finding)
os.system("pause")
我尝试调试它删除下载部分并打印所有匹配项,我得到一长串 (''
, '"">'
) 而不是类似于此的 URL:http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg
我哪里错了?
你的正则表达式模式不好。
我不确定你想做什么,我建议你使用 BeautifulSoup instead of playing with regex if you needs to parse HTML (because Regex can not really parse HTML).
但无论如何 - 使用正则表达式 - 这应该有效:
r'<IMG src=\"(https?:\/\/i[0-9]{3}\.photobucket\.com\/albums[^\"]+)\"[^>]+\/>'
https?:\/\/i[0-9]{3}\.photobucket\.com\/albums
用于过滤非 photobucket 图像,[^\"]+
更通用,只提取属性的最后一个 "
字符之前的所有内容。
示例:
<IMG src="http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg" foo="bar"/>
在 .group(1)
给予:
http://i774.photobucket.com/albums/myalbum/Emi998/mypicture.jpg
我认为您的正则表达式的以下版本应该可以工作:
请注意,我使用 \"
而不是 ""
,
我将 img src
替换为 img.+src
以支持 img alt="" src
,
而不是 [^\/]*
我使用 [^\/]+
来删除 \
,
的验证
对于 URL 的最后一部分,我还检查是否没有出现 "
、
然后,我没有检查紧跟在 "
之后的 >
,而是通过 .*
.
"
之后的可选其他字符
(?!(<img.+src=\"))http:\/\/i\d{3}\.photobucket\.com\/albums\/[^\/]+\/[^\/]+\/[^\/\"]+(?=\".*/>)
^^ ^^^
您可以使用 \d\d\d
或 [0-9]{3}
或 \d{3}
代替 [0-9][0-9][0-9]
,