1、目標網站
目標網站:https://so.gushiwen.org/shiwen/default.aspx?
2、爬蟲目的
爬取目標網站的文本,如古詩的內容,作者,朝代,並且保存到本地中。
3、爬蟲程序
# -*- coding:utf-8 -*- #爬取古詩網站 import requests import re #下載數據 def write_data(data): with open('詩詞.txt','a')as f: f.write(data) for i in range(1,10): #目標url地址 url = "https://so.gushiwen.org/shiwen/default.aspx?page={}".format(i) headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', 'Accept-Encoding': 'none', 'Accept-Language': 'en-US,en;q=0.8', 'Connection': 'keep-alive'} html = requests.get(url ,headers = headers).content.decode('utf-8') # print(html) p_title = '<p><a style="font-size:18px; line-height:22px; height:22px;" href=".*?" target="_blank"><b>(.*?)</b></a></p>' title = re.findall(p_title, html) # 提取內容 p_context = '<div class="contson" id=".*?">(.*?)</div>' context = re.findall(p_context, html, re.S) #提取年代 p_years = '<p class="source"><a href=".*?">(.*?)</a>' years = re.findall(p_years,html,re.S) #提取作者 p_author = '<p class="source"><a href=".*?">.*?</a><span>:</span><.*?>(.*?)</a>' author = re.findall(p_author,html) # print(context) # print(title) # print(years) # print(author) for j in range(len(title)): context[j] = re.sub('<.*?>', '', context[j]) #'gbk' codec can't encode character '\u4729' ,沒有這行會出現報錯 context[j] = re.sub(r'\u4729', '', context[j]) # print(title[j]) # print(years[j]) # print(author[j]) # print(context[j]) #寫入數據 write_data(title[j]) write_data('\n'+ years[j]) write_data(' :'+ author[j]) write_data(context[j]) print('下載第{}頁成功'.format(str(i)))
4、難點與思考
本次爬蟲難點在於,正則表達式的使用,如使用正則表達式匹配古詩正文、古詩作者、古詩標題。正則表達式的使用,需要找到需要匹配的內容的前項和后項,這樣才能精准的定位到需要匹配的內容。如匹配古詩正文:
# 提取內容 p_context = '<div class="contson" id=".*?">(.*?)</div>' context = re.findall(p_context, html, re.S)
需要匹配的內容是括號中的內容,前項是'<div class="contson" id=".*?">,后項是</div>。這里需要注意的地方有兩點,第一:id=”.*?“這里必須使用非貪婪模式,即加上?,如果不加?它會繼續匹配下一個內容,這樣就無法匹配到我們需要的內容;第二:(.*?)加?這里也使用了非貪婪模式,只匹配括號中的內容一次。而且匹配標題、作者年代、作者姓名,方法都類似,這里就不一一介紹了。