XPATH:针对HTML的一种特定的搜索工具 在python里用lxml
类比于文件夹的树状结构
i
mport requests
from lxml import etree
url = 'http://lol.178.com/index.html'
response = requests.get(url)
# XPATH /html/head/title
html_etree = etree.HTML(response.text)
# 使用XPATH
meta_ele_list = html_etree.xpath('/html/head/title/text()')[0] # 返回是一个列表,取第一个
print(title)
# 获取title内的文字,xpath函数叫text() /html/head/title/text()
# 如果找到了多个标签,需要找其中一个,写/html/head/meta[1] 1是第一个
title = html_etree.xpath('/html/head/meta[1]')
# 如果需要标签的属性信息/html/head/meta[1]/@charset
meta_ele_charset = html_etree.xpath('/html/head/meta[1]/@charset')[0]
# 对于树状结构很复杂的,用搜索模式://
# 通过属性定位标签://li[@class = 'md news'
# 上面语句的意思:找到所有的li标签并保留class = 'md news'的标签
li_ele = html_etree.xpath('//li[@class = "md news"]')
# 包含name属性的meta标签://meta[@name]
meta_list = html_etree.xpath('//meta[@name]')
print(meta_list)
# 若想查看name //meta[@name]@name
# 找到第一个ul标签://div[@class = 'bd']/ul
ul_list = html_etree.xpath('//div[@class = "bd"]/ul[1]')[0]
# ./li .表示当前文件夹下 ..选取当前节点的父节点
下载图片
import lxml
import requests
import os
def download_image(url):
response = requests.get(url)
filename = downloaddir + '/' + url.split('/')[-1]
with open(filename, 'wb') as f:
f.write(response.content)
if __name__ == '__main__':
# 创建文件夹
downloaddir = '下载的图片'
if not os.path.exists(downloaddir):
os.mkdir(downloaddir)
url = 'https://wxt.sinaimg.cn/mw1024/9d52c073gy1gc6p47vzy7j20qo140dnt.jpg'
download_image(url)
xpath 的搜索的class href 一定不能有空格,有就换一个属性