爬取網頁中的文章寫成本地txt檔案

2021-07-25 07:06:21 字數 1159 閱讀 7967

之前看了周浩暉的一下**,包括**催眠師系列,這個系列已經到了第三季但是網上好像沒找到txt檔案。只找到下方網頁中的文章,網頁看**不是很方便,所以決定爬下來做成txt檔案放在手機中看。

技術點:

beautifulsoup、urllib2

直接上**

#!/usr/bin/env python

# -*-coding:utf-8-*-

from bs4 import beautifulsoup

import html5lib

import urllib2

import sys

import codecs

strall='';

reload(sys)

sys.setdefaultencoding('utf-8')

for i in range(1,34):

urls=str('') +str(i) +str('.html')

html=urllib2.urlopen(urls)

htmldata=html.read()

soup=beautifulsoup(htmldata,'html.parser',from_encoding="gb18030") #這個網頁是gb2312編碼,所以要轉一下

#view_content_txt

titledata=soup.find ('div',id='view_content_txt')

ss=str(unicode(titledata))

lists=ss.split('')

lings=str(lists[1])

lists2=lings.split('')

print str(lists2[0])

strall+=str(lists2[0])

defwrittetxt

(content):

f = codecs.open('f:/python/1.txt', 'w', 'utf-8') #將拼接的字串寫到txt檔案中

f.write(content)

# print titledata

writtetxt(strall)

python初學者,**寫的很爛。請見諒

基於Scrapy爬取網頁文章

settings定義爬取的一些設定如下 coding utf 8 scrapy settings for jobbole project for simplicity,this file contains only settings considered important or commonly ...

簡單的爬蟲爬取文章

我們會用一些簡單的爬蟲去爬取 等,那麼在別人的 中我們的應選擇對應的標題等資料作為爬取的內容標桿 如以下 模擬瀏覽器發請求 connection connect jsoup.connect document doc connect.get elements select doc.select lis...

python如何爬取網頁中的文字

用python進行爬取網頁文字的 usr bin python coding utf 8 import requests import re 乙個網頁 url 模擬瀏覽器傳送http請求 response requests.get url 編碼方式 response.encoding 程式設計客棧 ...