選取本節的父節點的兄弟節點

只能說找了2個小時 xpath牛逼 css如果出個選擇父節點的屬性的就好了

import  requests
from bs4 import BeautifulSoup
import lxml
from lxml import html
from html.parser import HTMLParser
url='https://www.qidian.com/all?orderId=&style=1&pageSize=20&siteid=1&pubflag=0&hiddenField=0&page=1'
c=requests.get(url)
soup=lxml.etree.HTML(c.text)
tree=soup.xpath("//li[@class='lbf-pagination-item']/a[@class='lbf-pagination-page  lbf-pagination-current']/../following-sibling::li[1]")
# 轉爲string
print(len(tree))
for i in range(len(tree)):
    tree1 = html.tostring(tree[i])
    # 編碼'utf-8'
    tree2 = HTMLParser().unescape(tree1.decode('utf-8'))
    print(tree2)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章