转载自https://www.cnblogs.com/dyfblog/p/6073069.html

xpath获取URL列表:

以博客园首页为例,如果我们需要的数据是文章

的 url 列表,最好使用 XPath,见图

xpath与正则表达式的区别


#coding:utf8
from lxml import etree
import requests


url = 'http://www.cnblogs.com/'


response = requests.get(url)
response.encoding = 'utf8'
html = response.text


root = etree.HTML(html)
node_list = root.xpath("//div[@class='post_item_body']/h3/a")


for node in node_list:
    print node.attrib['href']




# 输出
'''
http://www.cnblogs.com/olivers/p/6073506.html
http://www.cnblogs.com/-free/p/6073496.html
...
'''



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-09
  • 2021-10-09
  • 2022-02-08
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-18
  • 2021-10-23
  • 2021-04-04
  • 2022-01-21
  • 2022-12-23
相关资源
相似解决方案