Python爬蟲入門 爬取某個網頁的小說內容

2021-10-02 10:02:15 字數 1887 閱讀 5846

匯入必要的包

import requests

import re

要爬的網頁

url =

''

模擬瀏覽器傳送http請求

response = requests.get(url)
改變編碼方式

response.encoding =

'utf-8'

獲取目標**的網頁原始碼

html = response.text
獲取**的名字

title = re.findall(r''

, html)[0

]

新建文字檔案儲存爬取內容

fb =

open

('%s.txt'

% title,

'w', encoding=

'utf-8'

)

爬取**的每個章節及網頁鏈結

dl = re.findall(r'.*?

', html, re.s)[0

]chapter_info_list = re.findall(r'(.*?)'

, dl)

for chapter_info in chapter_info_list:

chapter_url, chapter_title = chapter_info

chapter_url =

"%s "

% chapter_url

chapter_url = chapter_url.replace(

' ',

'')

chapter_response = requests.get(chapter_url)

chapter_response.encoding =

'utf-8'

chapter_html = chapter_response.text

chapter_content = re.findall(r'(.*?)

', chapter_html, re.s)[0

]

資料清洗

chapter_content = chapter_content.replace(

' ','')

chapter_content = chapter_content.replace(''

,'') chapter_content = chapter_content.replace(

'&t;',''

) chapter_content = chapter_content.replace(

'&t;;',''

)

**儲存到本地

fb.write(chapter_title)

fb.write(

'\n'

) fb.write(chapter_content)

fb.write(

'\n'

)print

(chapter_url, chapter_title)

python爬蟲 爬取Q房網房價

from lxml import etree 從lxml中匯入etree import requests import csv import time defwritecsv item 定義寫入函式 with open qfang.csv a encoding utf 8 as f writer c...

python爬蟲 爬取豆瓣網電影資訊

豆瓣網 如下 import requests import urllib.request if name main 指定ajax get請求的url 通過抓包進行獲取 url 定製請求頭資訊,相關的頭資訊必須封裝在字典結構中 headers import requests import urllib...

python爬蟲 爬取豆瓣網電影詳情

url 當滾輪滑動到底部時候 頁面會發起ajax請求 且請求一組電影詳情資料 當滾輪不滾動時候 頁面顯示的電影資料 通過瀏覽器位址列的url發起的請求是請求不到的 基於抓包工具進行全域性搜尋,鎖定動態載入資料對應的資料報即可,從資料報中可以提取請求的url和請求方式 請求引數 直接對位址列發起請求就...