# -*- coding:utf-8 -*-
import requests
from pyquery import pyquery
import re
import os
# 構造請求頭
headers =
# todo 1.根據**鏈結得到**目錄和對應的url
def get_catalogue(url):
# 傳送請求
response = requests.get(url=url, headers=headers)
response.encoding = "gbk" # 指定編碼
chapter_info_list = # 用來儲存獲取到的所有url和章節標題資料
doc = pyquery(response.text)
# print(doc)
pages_url = doc(".mt10 .book_list ul a").items()
# print(pages_url)
for page_url in pages_url:
title = page_url.text()
chapter_url = url + page_url.attr("href")
# print(title)
# print(chapter_url)
# 使用字典儲存url和title
chapter =
# 將字典新增到列表中
# print(chapter_info_list)
return chapter_info_list
# todo 2.根據章節目錄,抓取目錄對應的url指定的**正文頁面
def get_content(chapter_info_list):
for chapter_info in chapter_info_list:
# 傳送請求
response = requests.get(url=chapter_info["url"], headers=headers)
response.encoding = "gbk" # 指定編碼
doc_url = pyquery(response.text)
if response.status_code == 200: # 判斷請求是否成功
# 判斷**資料夾是否存在
if os.path.exists("至尊仙朝"):
pass
else:
os.makedirs("至尊仙朝")
content_list = doc_url("#htmlcontent")
content_lists = re.findall(r'[\u4e00-\u9fff]+', content_list.text())
print(content_lists)
print(chapter_info["title"], chapter_info["url"])
# 建立檔案 並將**正文寫入檔案中
with open("至尊仙朝/" + chapter_info["title"] + ".txt", "w", encoding="utf8") as file:
# 將內容一行一行的寫入檔案中
for content in content_lists:
file.write(content + "\n")
if __name__ == '__main__':
get_content(get_catalogue(""))
酷虎小說網爬蟲
import requests from pyquery import pyquery import re import os 構造請求頭 headers todo 1.根據 鏈結得到 目錄和對應的url def get catalogue url 傳送請求 response requests.ge...
python爬蟲17K小說網資料
python爬蟲17k 網資料 有一些庫可能沒有用,當時寫的時候參考了很多書籍資料,書籍裡用了,我沒有用,但是本著懶的原則,我就沒有特意把那些沒有用到的庫刪掉。因為我們老師對注釋特別強調,為了不讓老師抓錯,我就把除了import的 外的 都加了注釋。from bs4 import beautiful...
使用scrapy爬蟲,爬取起點小說網的案例
爬取的頁面為 爬取的 為凡人修仙之仙界篇,這邊 很不錯。正文的章節如下圖所示 其中下面的章節為加密部分,現在暫時無法破解加密的部分。唉.下面直接上最核心的 位於spiders中的核心 coding utf 8 import scrapy from qidian.items import qidian...