爬小说~那必然是雪中悍刀行~

啥也别说了 雪刀走起

天不生我李淳罡 剑道万古如长夜

先爬一章试试水

import requests                     #调用requests
from pyquery import PyQuery as pq   #pq调用PyQuery

#读网站
url = 'http://book.zongheng.com/chapter/672340/36898237.html'
r = requests.get(url)

#提取title和content
doc = pq(r.text)
title = doc('#readerFt > div > div.title > div.title_txtbox').text()
content = doc('#readerFt > div > div.content').text()

#保存
with open('d://第一章惊蛰.txt','a+') as f:
    f.write(title)
    f.write(content)
	f.write('\n')

撸完收工~

小二 上酒

================================================================
爬一卷~

技术活儿,当赏

import requests                     #调用requests
from pyquery import PyQuery as pq   #pq调用PyQuery

#访问目录
url = 'http://book.zongheng.com/showchapter/672340.html'
r = requests.get(url)
r.encoding = r.apparent_encoding

#目录内容可爬取
doc = pq(r.text)
links = doc('body > div.container > div:nth-child(2) > div.volume-list > div:nth-child(2) > ul > li a')

#头目录骗自己
header = {'user-agent':'Mozilla/5.0'}

#循环爬取title和content
for link in links.items():
    r = requests.get(link.attr.href,headers = header)
    r.encoding = r.apparent_encoding
    doc = pq(r.text)
    title = doc('#readerFt > div > div.title > div.title_txtbox').text()
    content = doc('#readerFt > div > div.content').text()

    with open('d://剑来.txt','a+') as f:
        f.write(title)
        f.write(content)
		f.write('\n''\n')

剑来

你可能感兴趣的:(python自学笔记,python自学练习)