<jupyter_start><jupyter_text>XPATH语法一层一层往下挖模式- '/'	从根节点选取  '//'	从匹配选择的当前节点选择文档中的节点，而不考虑它们的位置 **   '.'	选取当前节点 **   '..'	选取当前节点的父节点- '@'   提取属性和控制属性 ** - '[]'  属性容器  '*'	匹配任何元素节点- text() 以文本的方式返回提取的内容 **<jupyter_code>tree.xpath('//bookstore/book/title[@lang="engg"]/text()')
html = '''
<xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>
<div class="hahah">女朋友</div>
<book>
  <title lang="eng"><NAME></title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>
'''
tree = etree.HTML(html)
tree.xpath('//book[1]/title/text()')
tree.xpath('//div/text()')
html = '''
<xml version="1.0" encoding="ISO-8859-1">

<bookstore>
<div class="hahah">女朋友</div>
<book>
  <p>计欣茹生日快乐</p >
  <title lang="eng">计欣茹早日找到男朋友</title>
  <div class="hahah">男朋友</div>
  <price>29.99</price>
  <a href="www.baidu.com"></a>
  <a href="www.baidu2.com"></a>
</book>

<book>
  <title lang="eng">计欣茹早日结婚</title>
  <price>39.95</price>
</book>

</bookstore>
'''
tree = etree.HTML(html)
book = tree.xpath('//book')
for i in book:
    q = i.xpath('./title/text()')
    print(q)
tree.xpath('//book/a/@href')
import requests
from lxml import etree
q = requests.get('http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=q&oq=q&rsv_pq=84a96e6f00030b3e&rsv_t=1d87dvlv%2Bz4bpCwej4i38iO8ZIKYwujzz92i9Pmr42ePXxLYYx6uoH0Luqo&rqlang=cn&rsv_enter=0&rsv_sug=9')
tree = etree.HTML(q.text)
book = tree.xpath('//div[@id="page"]/a')
for i in book:
    w = i.xpath('./@href')[0]
    print(w)
import re
import requests
q = requests.get('http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=q&oq=q&rsv_pq=84a96e6f00030b3e&rsv_t=1d87dvlv%2Bz4bpCwej4i38iO8ZIKYwujzz92i9Pmr42ePXxLYYx6uoH0Luqo&rqlang=cn&rsv_enter=0&rsv_sug=9')
w = re.compile('<a\shref="(.*?)>',re.S)
e = re.findall(w,q.text)
print(e)
import requests
from lxml import etree
q = requests.get('http://www.17k.com/list/2822387.html')
tree = etree.HTML(q.text)
book = tree.xpath('//div[@class="Main List"]/dl[@class="Volume"][1]/dd/a')
for i in book:
    w = i.xpath('./@href')
    print(w)
    break
import requests
from lxml import etree
s = 0
q = requests.get('http://www.17k.com/list/2822387.html')
tree = etree.HTML(q.text)
book = tree.xpath('//div[@class="Main List"]/dl[@class="Volume"][1]/dd/a')
for i in book:
    w = i.xpath('./@href')[0]
    hrl = requests.get('http://www.17k.com'+w)
    s = s+1
    tree1 = etree.HTML(hrl.text)
    e = tree1.xpath('//body/div[@class="area"]/div[@class="read"]/div[2]/div[1]/div[@class="p"]/text()')
    d = str(e)
    with open('C:/Users/刘钰/Desktop/小说/asd'+str(s)+'.txt','w',encoding='utf-8') as f:
        f.write(d)
import requests
from lxml import etree
s = 0
q = requests.get('http://www.17k.com/list/2822387.html')
tree = etree.HTML(q.text)
book = tree.xpath('//div[@class="Main List"]/dl[@class="Volume"][1]/dd/a')
for i in book:
    w = i.xpath('./@href')[0]
    print('http://www.17k.com'+w)

import requests
from lxml import etree
file='C:/Users/刘钰/Desktop/qwertyuiop.txt'
re=requests.get('http://www.17k.com/list/2822387.html')
tree=etree.HTML(re.text)
a= tree.xpath('//div/dl[@class="Volume"]/dd/a')
for i in a:
    b=i.xpath("./@href")[0]
    c=('http://www.17k.com'+b)
    content=requests.get(c).text
    import re
    regex=re.compile('&#12288;&#12288;(.*?)<br /><br />',re.S)   
    result=re.findall(regex,content)
    print(result)
    d = str(result)
    with open(file,'a',encoding='utf-8') as f:
        f.write(d)

import requests
from lxml import etree
s = 0
q = requests.get('http://op.hanhande.com/shtml/meitu.shtml')
tree = etree.HTML(q.text)
book = tree.xpath('//*[@id="main"]/div[1]/div[2]/ul/li')
for i in book:
    w = i.xpath('./a/img/@src')[0]
    print(w)
    s = s+1
    e = w.split('.')[-1]
    hrl = requests.get(w).content
    with open('C:/Users/刘钰/Desktop/小说/图片'+str(s)+'.'+e,'wb') as f:
        f.write(hrl)
import requests
from lxml import etree
s = 0
q = requests.get('http://op.hanhande.com/shtml/meitu.shtml')
tree = etree.HTML(q.text)
book = tree.xpath('//*[@id="main"]/div[1]/div[2]/ul/li')
for i in book:
    w = i.xpath('./a/img/@src')[0]
    print(w)
    e = w.split('.')[-1]
    print(e)<jupyter_output>http://www.hanhande.com/upload/litimg/180117/091P612S5DDT.jpg
jpg
http://www.hanhande.com/upload/litimg/180112/09541112S5D3919.jpg
jpg
http://www.hanhande.com/upload/litimg/180105/09160112S5D4239.jpg
jpg
http://www.hanhande.com/upload/litimg/180104/092J212S5D94a.jpg
jpg
http://www.hanhande.com/upload/litimg/171230/09410512S5JG95.jpg
jpg
http://www.hanhande.com/upload/litimg/171228/09115Q2S5D32H.jpg
jpg
http://www.hanhande.com/upload/litimg/171208/09305212S5D5S2.jpg
jpg
http://www.hanhande.com/upload/litimg/171205/09301G2S5D2H6.jpg
jpg
http://www.hanhande.com/upload/litimg/171117/09420312S5D3328.jpg
jpg
http://www.hanhande.com/upload/litimg/171116/09393Q2S5D22S.jpg
jpg
http://www.hanhande.com/upload/litimg/171109/09210412S5D3S1.jpg
jpg
http://www.hanhande.com/upload/litimg/171108/09420612S5DGG.jpg
jpg
http://www.hanhande.com/upload/litimg/171031/09300112S5D1505.jpg
jpg
http://www.hanhande.com/upload/litimg/171019/093P912S5D94M.jpg
jpg
http://www.hanhande.com/upload/litimg/171007/100AG2S[...]
{'fraction_non_alphanumeric': 0.22151898734177214, 'fraction_numerical': 0.07997698504027619, 'mean_word_length': 4.27834008097166, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 2, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 26, 'xml': 9}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '651305', 'license': 'Apache-2.0', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2547, 'n_words': 296, 'path': 'XPath-Copy2.ipynb', 'repo_name': '17790793549/tongjier', 'size': '5220'}