鱼C论坛

 找回密码
 立即注册
查看: 9113|回复: 6

如何用正则表达式匹配网页中的超链接?

[复制链接]
发表于 2016-2-1 13:38:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位鱼友,现有一个网页http://www.researchmfg.com/plastic-injection/,我想把这文章都爬下来,想用正则表达式把<h4>标题下的超链接都匹配下来,但是不知道如何写,谁能写个看看,让我学习学习,感谢~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-1 13:58:05 | 显示全部楼层
  1. <li><a href="(.+?)" target="_self">
复制代码

匹配结果:
  1. C:\Python34\python.exe E:/python/tmp.py
  2. ['http://www.researchmfg.com/2010/07/thermo-plastics/', 'http://www.researchmfg.com/2010/07/plastic-rheological-property/', 'http://www.researchmfg.com/2010/07/3-elements-plastic-injection/', 'http://www.researchmfg.com/2010/07/plastic-forming-step/', 'http://www.researchmfg.com/2010/07/plastic-injection-temperature-pressure/', 'http://www.researchmfg.com/2010/07/plastic-injection-screw-speed/', 'http://www.researchmfg.com/2010/07/plastic-injection-time/', 'http://www.researchmfg.com/2010/07/plastic-injection-defect-cause-action/', 'http://www.researchmfg.com/2010/08/plastic-mechanical-properties/', 'http://www.researchmfg.com/2010/08/plastic-mfi/', 'http://www.researchmfg.com/2010/08/7-methods-to-detect-re-grind-resin-plastic/', 'http://www.researchmfg.com/2010/08/plastic-mfi-re-grinding-resin/', 'http://www.researchmfg.com/2010/08/apply-re-grinding-resin-degrade-the-strength/', 'http://www.researchmfg.com/2010/08/plastic-parts-cracking-defect-and-solution/', 'http://www.researchmfg.com/2010/08/the-possibility-reason-for-brittle-plastic-parts/']

  3. Process finished with exit code 0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-1 14:06:14 | 显示全部楼层
本帖最后由 aptdo 于 2016-2-1 14:08 编辑

感谢,但是这并没有匹配完,带"target="_self""只有十几个,而这个网页有8个分组,总共有40多篇文章。我是想能不能先按<h4>标签锁定范围,然后把<h4>下面的url都提取出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-1 14:07:27 | 显示全部楼层


感谢,但是这并没有匹配完,带"target="_self""只有十几个,而这个网页有8个分组,总共有40多篇文章。我是想能不能先按<h4>标签锁定范围,然后把<h4>下面的url都提取出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-2-1 16:31:54 | 显示全部楼层
aptdo 发表于 2016-2-1 14:07
感谢,但是这并没有匹配完,带"target="_self""只有十几个,而这个网页有8个分组,总共有40多篇文章。 ...

没看仔细, 嘿嘿~
这里可以另辟蹊径, 先匹配俩<hr />里面的内容, 再匹配url
我贴上全部代码, 用了requests库
  1. import re
  2. import requests

  3. re_url1 = re.compile(r'<hr />(.+)<hr />', re.DOTALL)
  4. re_url2 = re.compile(r'(http://www\.researchmfg.+?)"')

  5. url = 'http://www.researchmfg.com/plastic-injection/'

  6. res = requests.get(url).text

  7. print(re_url2.findall(re_url1.findall(res)[0]))
  8. print(len(re_url2.findall(re_url1.findall(res)[0])))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2016-2-1 18:00:02 | 显示全部楼层
hldh214 发表于 2016-2-1 16:31
没看仔细, 嘿嘿~
这里可以另辟蹊径, 先匹配俩里面的内容, 再匹配url
我贴上全部代码, 用了re ...

感谢,等我回去试一下,办公室要用代理,我安装不了requests
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-2-11 12:24:28 | 显示全部楼层
牛人还是多啊,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-19 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表