xpath解析小案例

2021-10-09 18:51:31 字數 1550 閱讀 9772

# -*- coding: utf-8 -*-

# @time : 2020/9/25 9:34

# @author : awayasaway

# @file : 全國城市名稱爬取.py

# @ide : pycharm

# @url :

import requests

from lxml import etree

import xlrd

deftest()

:# headers =

# url = ''

# page_text = requests.get(url=url, headers=headers).text

# tree = etree.html(page_text)

# hot_li_list = tree.xpath('//div[@class="bottom"]/ul/li')

# all_city_names =

# # 熱門城市的名稱

# for li in hot_li_list:

# hot_city_name = li.xpath('./a/text()')[0]

# # print(hot_city_name)

## # 解析全部城市的名稱

# city_names_list = tree.xpath('//div[@class="bottom"]/ul/div[2]/li')

# for li in city_names_list:

# city_name = li.xpath('./a/text()')[0]

# # print(city_name)

# print(all_city_names, '\n', len(all_city_names))

headers =

url =

''page_text = requests.get(url=url, headers=headers)

.text

tree = etree.html(page_text)

# 解析到熱門城市和所有城市對應的a標籤

# //div/ul/li/a 熱門城市a標籤的層級關係

# //div/ul/div[2]/li/a 全部城市a標籤層級關係

# 儲存全部城市的a_list

all_city_names =

a_list = tree.xpath(

'//div[@class="bottom"]/ul/li/a | //div/ul/div[2]/li/a'

)# 使用或 |

for a in a_list:

city_name = a.xpath(

'./text()')[

0]print

(all_city_names,

len(all_city_names)

)if __name__ ==

'__main__'

: test(

)

xpath案例 解析所有城市名稱

第一種方法把熱門城市和所有城市分開來考慮 import requests from lxml import etree if name main headers url page text requests.get url url,headers headers text tree etree.ht...

Xpath解析資料

xpath,xml path language。是一種小型的查詢語言,是一門在xml文件中查詢的語言。優點 可以在xml中查詢資訊 支援html的查詢 可通過元素和屬性進行導航。xpath的樹形結構 使用xpath選取節點 表示式描述nodename 選取此節點的所有子節點 從根節點擊擇 從匹配選擇...

xpath解析基礎

import requests from lxml import etree if name main headers ua偽裝 例項化好了乙個etree物件,且將被解析的檔案載入到了物件中 tree etree.parse test.html r tree.xpath html body div ...