在Python中,我有一个包含网站源代码的字符串。在这个源代码中,如果标记包含一个特定的子字符串,我想获取标记中的链接。在
输入如下:
AnyKindOfString <a href="http://www.link-to-get.com">SearchString</a> AndEvenMoreString
所以我想告诉Python的是在字符串内的所有标记中搜索SearchString
,并将第一个找到的http://www.link-to-get.com
返回给我。在
只有当SearchString
在标记内时,这才有效,而且如果“SearchString”是http://www.link-to-get.com
的一部分(子字符串),它也应该起作用。在
我在寻找一个答案,比如30多分钟就知道了,我发现Python的唯一方法就是从一个字符串中提取每个(或仅外部或内部)链接。在
有人有主意吗?在
提前通知!在
我已经粗略地列出了一些应该可以工作的代码,至少在你给出的示例字符串上是有效的。在
在python2.7中使用beauthoulsoup3.2.1
问题似乎是我用beauthoulsoup3.2.1测试了上述内容,它只在python2.x中运行,而您使用的是python3.4,因此出现了错误。
如果你安装BeautifulSoup4并尝试下面的代码,它应该可以工作。还要注意的是,beauthoulsoup4可以在2.x和3.x中工作
请注意,以下内容尚未测试。在
^{pr2}$可以在
pyquery
(http://pythonhosted.org/pyquery/index.html)+lxml
(http://lxml.de/tutorial.html)的帮助下完成,如下所示相关问题 更多 >
编程相关推荐