python3 获取html内容中的a链接与内容

# -*- coding:utf8 -*-
import requests
import lxml.html

# 获取html内容中的a链接与内容
chapters_url = "http://www.civil.tsinghua.edu.cn/ce/83.html"
html = requests.get(chapters_url).content
selector = lxml.html.fromstring(html)
res = selector.xpath("///a/text()")
res1 = selector.xpath("///a/@href")
# print(res)
# print(res1)

for i,s in enumerate(res):
    print(res1[i])
    print(s)

  

posted @ 2020-06-23 11:05  ToDarcy  阅读(1423)  评论(0编辑  收藏  举报