gpt4 book ai didi

python - 分解 HTML 以链接文本和目标

转载 作者:太空狗 更新时间:2023-10-29 15:22:20 25 4
gpt4 key购买 nike

给定一个 HTML 链接

<a href="urltxt" class="someclass" close="true">texttxt</a>

我怎样才能隔离 url 和文本?

更新

我正在使用 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

我明白了

*link content: None  and attr: [(u'href', u'_redirectGeneric.asp?genericURL=/root    /support.asp')]*  ...
...

为什么我缺少内容?

编辑:按照建议详细阐述“卡住”:)

最佳答案

使用Beautiful Soup .自己动手比看起来更难,最好使用久经考验的模块。

编辑:

我想你想要:

soup = BeautifulSoup.BeautifulSoup(urllib.urlopen(url).read())

顺便说一句,尝试在那里打开 URL 是个坏主意,因为如果它出错了,它会变得丑陋。

编辑 2:

这应该会显示页面中的所有链接:

import urlparse, urllib
from BeautifulSoup import BeautifulSoup

url = "http://www.example.com/index.html"
source = urllib.urlopen(url).read()

soup = BeautifulSoup(source)

for item in soup.fetchall('a'):
try:
link = urlparse.urlparse(item['href'].lower())
except:
# Not a valid link
pass
else:
print link

关于python - 分解 HTML 以链接文本和目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/285938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com