考虑如下字符串:
string = "<p>Hello World</p><a href="http://example.com">More Examples</a><a href="http://example2.com">Even More Examples</a>"
Run Code Online (Sandbox Code Playgroud)
我怎么能用Python在锚标记的href中提取网址?就像是:
>>> url = getURLs(string)
>>> url
['http://example.com', 'http://example2.com']
Run Code Online (Sandbox Code Playgroud)
谢谢!
关于:使用Python在文本中查找超链接(与twitter相关)
如何只提取网址,以便将其放入列表/数组?
让我澄清一下,我不想将URL解析成碎片.我想从字符串的文本中提取URL以将其放入数组中.谢谢!
我想删除字符串中的所有URL(用""替换它们)我搜索过但却找不到我想要的东西.
例:
text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/
Run Code Online (Sandbox Code Playgroud)
我希望结果如下:
text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud) 我只是想在字符串中找到并替换所有出现的twitter url(tweet):
输入:
这是一条带有网址的推文:http://t.co/0DlGChTBIx
输出:
这是一条带有网址的推文:
我试过这个:
p=re.compile(r'\<http.+?\>', re.DOTALL)
tweet_clean = re.sub(p, '', tweet)
Run Code Online (Sandbox Code Playgroud) 我希望这个问题不是RTFM问题.我正在尝试编写一个Python脚本,从标准HTML网页(<link href...标签)中提取链接.我在网上搜索匹配的regexen,发现了许多不同的模式.是否有任何商定的标准正则表达式匹配链接?
亚当
更新: 我实际上正在寻找两个不同的答案:
Igal Serban和cletus!) 给出像这样的HTML链接
<a href="urltxt" class="someclass" close="true">texttxt</a>
Run Code Online (Sandbox Code Playgroud)
我该如何隔离网址和文字?
更新
我正在使用Beautiful Soup,我无法弄清楚如何做到这一点.
我做到了
soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url))
links = soup.findAll('a')
for link in links:
print "link content:", link.content," and attr:",link.attrs
Run Code Online (Sandbox Code Playgroud)
我明白了
*link content: None and attr: [(u'href', u'_redirectGeneric.asp?genericURL=/root /support.asp')]* ...
...
Run Code Online (Sandbox Code Playgroud)
为什么我错过了内容?
编辑:详细说明'卡住'建议:)