[Asm] 纯文本查看 复制代码 import requests
import os
from lxml import etree
headers ={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36"
}
url = "https://www.runoob.com/python/python-tutorial.html"
page_text = requests.get(url=url,headers=headers).text
tree =etree.HTML(page_text)
a_list = tree.xpath('.//div[@id="leftcolumn"]/a')[4:23]
if not os.path.exists('./cainiao'):
os.mkdir('./cainiao')
for a in a_list:
detail_url = "https://www.runoob.com"+a.xpath('./@href')[0]
name = a.xpath('./text()')[0]
name= name.strip()
# print(detail_url,name)
detail_page = requests.get(url=detail_url,headers=headers).text
detail_tree = etree.HTML(detail_page)
detail_text = detail_tree.xpath('.//div[@id="content"]//text()')
path = './cainiao/' + name+ ".txt"
for text in detail_text:
with open(path,"a",encoding="utf-8") as pf:
pf.write(text)
print(name,'下载成功') |