小白温故而知新(request,path练习)多谢论坛友人指点
importosimportrequests
fromlxml importetree
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/@href')
sep2 = requests.get(url=detail_url,headers=headers).text
tree2 = etree.HTML(sep2)
book_name = tree2.xpath('//div[@class="book-info "]/h1//text()')
li_list= tree2.xpath('//*[@id="j-catalogWrap"]/div/div/ul/li')
ifnotos.path.exists('./dafeng/'):
os.mkdir('./dafeng/')
forli in li_list:
detai_url_page = "https:"+li.xpath('./a/@href')
detai_url_page_name = li.xpath('./a/text()') + ".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 pagein page_deatil:
with open(path,"a",encoding="utf-8") as pf:
pf.write(page)
print(book_name,"下载完成") 多谢分享,学习!
页:
[1]