**(爬取起点免费小说)
爬取的小说:
代码:
import requests
from lxml import etree
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36’
}
response = requests.get(“https://book.qidian.com/info/1013432619”).text
html = etree.HTML(response)
link = html.xpath(’//ul[@class=“cf”]/li/a/@href’)
temp = []
for i in link:
start_url = “https:”
new_url = start_url + i
temp.append(new_url)
for a in temp:
response = requests.get(a, headers=headers).text
html = etree.HTML(response)
chapter_name = html.xpath(’//div[@class=“text-head”]/h3/text()’)
content = html.xpath(’//div[@class=“read-content j_readContent”]/p/text()’)
chapter_names = “”.join(chapter_name)
conn = “”.join(content).replace("\u3000", “”)
# print(chapter_name, conn)
with open(‘d:\m.txt’, ‘a’, encoding=“utf-8”)as f:
f.write(chapter_names)
f.write(’\n\n’)
f.write(conn)
结果: