抱歉,如果有重复,我进行了搜索,但找不到答案。我正在编写一个刮板,以刮擦由Web服务器提供的默认目录索引页。的HTML看起来像这样
<html>
<head><title>Index of /Mysongs</title></head>
<body bgcolor="white">
<h1>Index of /Mysongs</h1><hr><pre><a href="../">../</a>
<a href="Mysong1.mkv">Mysong1.mp3</a> 10-May-2016 07:24 183019
<a href="Mysong2.mkv">Mysong2.ogg</a> 10-May-2016 07:27 177205
该href
链接看起来仅是文本,而不是url(<a href="Mysong2.mkv">
),但指向文本时,它会在浏览器的状态栏中显示该链接(http://127.0.0.1/Mysongs/Mysong2.ogg
)
我试图使用beautifulsoup提取网址,像这样
#!/usr/bin/python
import httplib2
import sys
from BeautifulSoup import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request(sys.argv[1])
for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
print link.get('href')
而且我无法获得像这样的链接http://127.0.0.1/Mysongs/Mysong2.ogg
,而只有<a href="Mysong1.mkv">Mysong1.mp3</a> 10-May-2016 07:24
我应该使用sys.argv[1]
构建类似的href链接吗
print sys.argv[1] + link.get('href')
还是有一些更好的方法来做到这一点?
编辑::当前输出为
Mysong1.mp3
Mysong2.ogg
预期产量:
http://127.0.0.1/Mysong1.mp3
http://127.0.0.1/Mysong1.0gg
是的,您唯一的选择是添加基本URL。但是不要这样添加:
print sys.argv[1] + link.get('href')
用这个:
from urlparse import urljoin
urljoin('http://something.com/random/abc.html', '../../music/MySong.mp3')
在您的方法中,可能无法识别和处理相对路径,请对其进行urljoin
处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句