网页其中的源代码:
<dt>本期版面导航</dt>
<div class="dd-box">
<dd>
<a class="page-name" href="index.html?date={<!-- -->{jdate}}&page={<!-- -->{pnumber}}">{<!-- -->{pnumber}}版:{<!-- -->{pname}}</a>
<dt>本版新闻列表(<span id="news-num">0</span>)</dt>
<div class="dd-box news-list">
<dd><a href="detail.html?date={<!-- -->{jdate}}&id={<!-- -->{id}}&page={<!-- -->{pageNo}}" target="_blank"><i>●</i>{<!-- -->{title}}</a></dd>
Python代码:
import requests
import bs4
import os
import datetime
import time
def fetchUrl(url):
'''
功能:访问 url 的网页,获取网页内容并返回
参数:目标网页的 url
返回:目标网页的 html 内容
'''
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
}
r = requests.get(url, headers=headers)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
def getPageList(year, month, day):
'''
功能:获取当天报纸的各版面的链接列表
参数:年,月,日
'''
url = 'https://www.shobserver.com/staticsg/res/html/journal/index.html?date=' + year + '-' + month + '-' + day + '&page=01'
html = fetchUrl(url)
bsobj = bs4.BeautifulSoup(html, 'html.parser')
pageList = bsobj.find('div', attrs={'class': 'dd-box'}).find_all('dd')
linkList = []
for page in pageList:
tempList = page.find_all('a')
for temp in tempList:
link = temp["href"]
if 'index.html' in link:
url = 'https://www.shobserver.com/staticsg/res/html/journal/' + link
linkList.append(url)
return linkList
def getTitleList(year, month, day, pageUrl):
'''
功能:获取报纸某一版面的文章链接列表
参数:年,月,日,该版面的链接
'''
html = fetchUrl(pageUrl)
bsobj = bs4.BeautifulSoup(html, 'html.parser')
titleList = bsobj.find('div', attrs={'class': 'dd-box news-list'}).find_all('dd')
linkList = []
for title in titleList:
tempList = title.find_all('a')
for temp in tempList:
link = temp["href"]
if 'detail.html' in link:
url = 'https://www.shobserver.com/staticsg/res/html/journal/' + link
linkList.append(url)
return linkList
def getContent(html):
'''
功能:解析 HTML 网页,获取新闻的文章内容
参数:html 网页内容
'''
bsobj = bs4.BeautifulSoup(html, 'html.parser')
# 获取文章 标题
title = bsobj.find_all('div', attrs={'class': 'con-title'})
content1 = ''
for p1 in title:
content1 += p1.text + '\n'
# print(content1)
# 获取文章 内容
pList = bsobj.find_all('div', attrs={'class': 'txt-box'})
content = ''
for p in pList:
content += p.text + '\n'
# print(content)
# 返回结果 标题+内容
resp = content1 + content
return resp
def saveFile(content, path, filename):
'''
功能:将文章内容 content 保存到本地文件中
参数:要保存的内容,路径,文件名
'''
# 如果没有该文件夹,则自动生成
if not os.path.exists(path):
os.makedirs(path)
# 保存文件
with open(path + filename, 'w', encoding='utf-8') as f:
f.write(content)
def download_rmrb(year, month, day, destdir):
'''
功能:网站 某年 某月 某日 的新闻内容,并保存在 指定目录下
参数:年,月,日,文件保存的根目录
'''
pageList = getPageList(year, month, day)
for page in pageList:
titleList = getTitleList(year, month, day, page)
for url in titleList:
# 获取新闻文章内容
html = fetchUrl(url)
content = getContent(html)
# 生成保存的文件路径及文件名
temp = url.split('=')[-2].split('&')[0].split('-')
pageNo = temp[0]
titleNo = temp[0] if int(temp[0]) >= 10 else '0' + temp[0]
path = destdir + '/' + year + month + day + '/'
fileName = year + month + day + '-' + pageNo + '-' + titleNo + '.txt'
# 保存文件
saveFile(content, path, fileName)
if __name__ == '__main__':
'''
主函数:程序入口
'''
# 爬取指定日期的新闻
newsDate = input('请输入要爬取的日期(格式如 20210916 ):')
year = newsDate[0:4]
month = newsDate[4:6]
day = newsDate[6:8]
download_rmrb(year, month, day, 'D:02/cqrb')
print("爬取完成:" + year + month + day)
劳烦大神指导解决,万分感谢。