吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1236|回复: 1
收起左侧

[已解决] 小白温故而知新(request,path练习)多谢论坛友人指点

  [复制链接]
lihu5841314 发表于 2021-5-31 11:44
[Asm] 纯文本查看 复制代码
import  os
import  requests
from  lxml import  etree


url = "https://www.qidian.com/all"

headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
sep1=requests.get(url=url,headers=headers).text
tree = etree.HTML(sep1)
detail_url ="https:"+tree.xpath('//div[@class="all-book-list"]//a[1]/@href')[0]
sep2 = requests.get(url=detail_url,headers=headers).text
tree2 = etree.HTML(sep2)
book_name = tree2.xpath('//div[@class="book-info "]/h1//text()')[0]
li_list= tree2.xpath('//*[@id="j-catalogWrap"]/div[2]/div[1]/ul/li')
if  not  os.path.exists('./dafeng/'):
         os.mkdir('./dafeng/')

for  li in li_list:
   detai_url_page = "https:"+li.xpath('./a/@href')[0]
   detai_url_page_name = li.xpath('./a/text()')[0] + ".txt"
   # print(detai_url_page,detai_url_page_name)
   sep3 = requests.get(url=detai_url_page,headers=headers).text
   tree3 = etree.HTML(sep3)
   page_deatil = tree3.xpath('//div[@class="read-content j_readContent"]//text()')
   path = './dafeng/'+detai_url_page_name
   print(detai_url_page_name, "下载成功")
   for page  in page_deatil:
       with open(path,"a",encoding="utf-8") as pf:
           pf.write(page)
print(book_name,"下载完成")

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

806785900 发表于 2021-6-16 14:16
多谢分享,学习!

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
89684828 + 1 + 1 用心讨论,共获提升!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 02:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表