作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码采用 URL 并返回原始 url 页面上包含的页面的链接列表。
import urllib
import lxml.html
def getSubLinks(url):
sublinks = []
connection = urllib.urlopen(url)
dom = lxml.html.fromstring(connection.read())
for link in dom.xpath('//a/@href'):
sublinks.append(link)
return sublinks
这似乎有效,但对于同一域上的页面,它会从 URL 中删除域,而这不是我想要的。我想找回完整的未更改的链接。例如,在网页上使用这个:
返回列表(以及更多):
['http://www.altoonativetravel.com/', 'index.htm', '2015-16html/fixtures.html', .....
但是,正如您所看到的,前面的“http://www.nufc.com”已从“2015-16html/fixtures.html”和其他内容中删除,而我不希望这种情况发生,我想要“http://www.nufc.com/2015-16html/fixtures.html” '。我该如何解决这个问题?
最佳答案
您可以使用以下内容:
import urllib
import lxml.html
def getSubLinks(url):
sublinks = []
connection = urllib.urlopen(url)
dom = lxml.html.fromstring(connection.read())
for link in dom.xpath('//a/@href'):
if not link.startswith('http'):
sublinks.append(url+link)
else:
sublinks.append(link)
return sublinks
调用函数时,请使用 getSubLinks('http://www.nufc.com/')
(请注意 URL 末尾的 /
)。
此循环遍历页面上 a
标记的每个 href
属性。对于每个链接,如果链接不是以"http"
开头,则会附加url+link
,即"http://www.nufc。 com/"+ 链接
.这将生成您想要的结果集。
关于python - 如何在 Python 中使用 lxml 从网页查找所有(完整)子链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34615152/
我是一名优秀的程序员,十分优秀!