【发布时间】:2016-10-24 18:27:19
【问题描述】:
我有一个标题数据集,例如
http://www.stackoverflow.com/lifestyle/tech/this-is-a-very-nice-headline-my-friend/2013/04/26/acjhrjk-2e1-1krjke4-9el8c-2eheje_story.html?tid=sm_fb
http://www.stackoverflow.com/2015/07/15/sports/baseball/another-very-nice.html?smid=tw-somedia&seid=auto
http://worldnews.stack.com/news/2013/07/22/54216-hello-another-one-here?lite
http://www.stack.com/article_email/hello-one-here-that-is-cool-1545545554-lMyQjAxMTAHFJELMDgxWj
http://www.stack.com/2013/11/13/tech/tricky-one/the-real-one/index.html
http://www.stack.com/2013/11/13/tech/the-good-one.html
http://www.stack.com/news/science-and-technology/54512-hello-world-here-is-a-weird-character#b02g07f20b14
我需要从这些链接中提取正确的标题,即:
- 这是一个非常好的标题-我的朋友
- 另一个非常好的
- 你好-另一个-这里
- 你好,这里很酷
- 真正的人
- 最好的人
- hello-world-here-is-a-weird-character
所以该规则似乎找到了word1-word2-word3- 形式的最长 字符串,该字符串的左右边框有一个/,并且没有考虑 p>
- 多于 3 位的单词(例如第一个链接中的
acjhrjk-2e1-1krjke4-9el8c-2eheje,或第三个链接中的54216, - 不包括
.html之类的内容。
如何在 Python 中使用 regex 做到这一点?不幸的是,我相信正则表达式是唯一可行的解决方案。 yurl 或urlparse 等包可以捕获url的路径,但后来我又回到使用正则表达式来获取标题..
非常感谢!
【问题讨论】:
标签: python regex string url-parameters urlparse