python 正则表达式findall函数查找网页中所有的URL
高洛峰
高洛峰 2016-11-11 13:13:25
0
2
1010

在做 python practice book 的习题,完成如下问题:
Problem 8: Write a program links.py that takes URL of a webpage as argument and prints all the URLs linked from that webpage.
要求使用 python 的 re 模块。

遇到的问题:正则表达式 (src|href)\=\".*?\" 在 re.findall 无法返回数组 以URL 组成的数组,而是返回了['src', 'src', 'href', 'href', 'href', 'href', 'href', 'href', 'href', 'href', 'href', 'src', 'src', 'src', 'href', 'href'...]


高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

répondre à tous(2)
学霸

正则表达式提取网页内容太麻烦,容易出错。推荐用beautifulsoup以及xpath

三叔

findall得到的是(...)所匹配的部分; 建议这样正则修改为这样(src|href)\=(\.*?)\", 你能看到它会返回被括号括起来的匹配部分;

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!