网页其中的源代码:
<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)
劳烦大神指导解决,万分感谢。
import requests
import bs4
import os
import datetime
import time
import json
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 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):
'''
功能:网站 某年 某月 某日 的新闻内容,并保存在 指定目录下
参数:年,月,日,文件保存的根目录
'''
url = 'https://www.shobserver.com/staticsg/data/journal/' + year + '-' + month + '-' + day + '/navi.json'
html = fetchUrl(url)
jsonObj = json.loads(html)
for page in jsonObj["pages"]:
pageName = page["pname"]
pageNo = page["pnumber"]
print(pageNo, pageName)
for article in page["articleList"]:
title = article["title"]
subtitle = article["subtitle"]
pid = article["id"]
url = "https://www.shobserver.com/staticsg/data/journal/" + year + '-' + month + '-' + day + "/" + str(pageNo) + "/article/" + str(pid) + ".json"
print(pid, title, subtitle)
html = fetchUrl(url)
cont = json.loads(html)["article"]["content"]
bsobj = bs4.BeautifulSoup(cont, 'html.parser')
content = title + subtitle + bsobj.text
print(content)
path = destdir + '/' + year + month + day + '/' + str(pageNo) + " " + pageName + "/"
fileName = year + month + day + '-' + pageNo + '-' + str(pid) + "-" + title + '.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, 'cqrb')
print("爬取完成:" + year + month + day)
这个网站的内容是动态加载出来的,并非人民日报那样的静态网页(就是说数据是通过其他请求获取到,然后加载到网页中的)
我看了一下,这个要比人民日报简单一些,它的版面列表和文章列表放在了同一个请求中(人民日报每个版面要单独请求一次文章列表)
上面的代码简单调整了一下,仅供参考
这个网页是动态更新的要用selenium 的 webdriver 爬取
requests只能获取网页的静态源代码,动态更新的取不到
download_rmrb(year, month, day, 'D:02/cqrb')这句路径写错了,改成'D:/02/cqrb'
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y