學習python第一天,第一次學習腳本語言還不習慣,不能看變量類型好不爽,沒有括號好不爽,果然java和scala寫多了嗎
想寫個小程序練練手,想起看小說廣告很多很不方便,就寫了個爬蟲程序用於爬取網上的小說。畢竟興趣是第一生產力!
小說來源,新筆趣閣。代碼很短
以下代碼使用的是python3.6,編輯器用的Pycharm。
用到了BeautifulSoup庫。此庫用於解析HTML文件。關於此庫這次只是粗略使用,很多都是摸索和google,待瀏覽完使用手冊和寫更多的項目后再開文章進行整理
關於此庫的安裝用PIP即可,Pycharm下可以選擇File -> Setting -> Project pythonlearn -> Project Interpreter的右邊加號進行搜索添加
首先我隨便搜索了本小說,打開章節網頁https://www.xxbiquge.com/12_12914/,右鍵查看網頁源代碼,看了一下哇那么簡單!那今天就爬你啦!筆者用的瀏覽器為Google Chrome
於是首先百度了下怎么用python3拉取html下來,利用lxml進行解析
如果沒有安裝lxml則會報錯!安裝lxml參考BeautifulSoup
1 import urllib.request 2 from bs4 import BeautifulSoup 3 url = 'https://www.xxbiquge.com/12_12914/' 4 res = urllib.request.urlopen(url) 5 html = res.read().decode('utf-8') 6 soup = BeautifulSoup(html,'lxml') 7 print(soup)
以上是代碼,可以看到直接打出了整個HTML里的內容。
然后我想提取出里面關於章節的超鏈接,我就需要解析這個HTML。於是百度了下。BeautifulSoup決定就是你了,去吧!
#找到所以標簽為a的內容 l = soup.find_all('a')
利用上面的代碼找到里面所有標簽為a的內容,里面包含我們所需要的鏈接和其他的一些干擾信息,因此用正則寫了個簡單的過濾。pyhton3要用正則需要import re
其中n.string為提取標簽中的內容
n.get('href')為提取當中鏈接的值
以上內容理解可能有誤,待筆者仔細研究后詳細說明
pattern = re.compile('第.+章') for n in soup.find_all('a'): if(pattern.match(n.string)): print(n.get('href')) print(n.string)
過濾完發現其中的提取的鏈接只有后面一部分,因此需要自己補上前面的
然后再依次瀏覽每個章節的鏈接,從中提取章節內容,很簡單以下不再繼續闡述
直接貼出所有代碼
import urllib.request from bs4 import BeautifulSoup import re #將字符串寫入文本中,這里要指定encoding格式,不然會報錯。‘a’表示繼續寫入,不覆蓋之前的內容
#關於with的語法筆者會研究后再整理
def writeFile(str,file): if file: with open(file, 'a', encoding='utf-8') as f: f.write(str) else: with open(file, 'a', encoding='utf-8') as f: f.write(str) #主函數 #輸出的txt路徑。筆者選擇了當前目錄下 outputTxtFile = '寒門崛起' #章節目錄的網址 contentUrl = 'https://www.xxbiquge.com/12_12914/' #網址前綴 url = 'https://www.xxbiquge.com' res = urllib.request.urlopen(contentUrl) html = res.read().decode('utf-8') soup = BeautifulSoup(html,'lxml') pattern = re.compile('第.+章') for chapter in soup.find_all('a'): if(pattern.match(chapter.string)): readRes = urllib.request.urlopen(url + chapter.get('href')) readHtml = readRes.read().decode('utf-8') readSoup = BeautifulSoup(readHtml, 'lxml') writeFile(chapter.string+'\n',outputTxtFile) for k in readSoup.find_all(id = 'content'): str='' for line in k: if not (line.string is None): str = str + line.string + '\n'
for line in k.stripped_strings:
str = str + line + '\n'
writeFile(str,outputTxtFile)
print('導入 '+chapter.string+' 完成')
以上代碼還有兩個BUG
第一是導出的文件前面有四個空格,很奇怪,怎么都去不掉,筆者繼續研究下
在研讀過BeautifulSoup使用手冊后,BUG已經解決了
可用.stripped_string提取中某個tag下所有文本信息並去除空格(tag我暫且理解為一個節點),代碼也做了修改
果然說明手冊才是王道,抽空繼續學習。
這個是手冊的中文地址https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
第二是他會導出最新章節放在前面沒有過濾掉,這個筆者懶得改了,畢竟不是生產項目只為學習
