将爬取的特定内容保存为html文件

1.特定内容的爬取需要用到bs4 中的BeautifulSoup

soup = BeautifulSoup(frist_r, 'html.parser')
item_a = soup.find_all('div', class_="ewb-main")
file_name = soup.find('h3', class_="article-title")
file_name = file_name.text

item_a获取特定内容,没错是全获取了,如果在保存的html文件中只看见标题呀,那就等一小会。

file_name获取标题文本,在翻页时需要拼接,+是字符串与字符串拼接,注意转换。

html = \
    '''
        <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>Title</title>
            </head>
            <body>
                {}
            </body>
        </html>
    '''.format(item_a)
 if not os.path.exists('{}'.format(url)):
     os.mkdir(r'{}'.format(url))
try:
    with open('{}\{}.html'.format(dir_name, file_name), 'w', encoding='utf-8') as f:
        f.write(html)
except Exception as e:
    print('文件名错误')

这就是写入html文件的标准格式,到这一步基本就可以满足所有要求,不过需要注意一点是不能将页面的url保存为文件名,因为其中包含:。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常好,您需要使用 Python 的 requests 或 urllib 库、os 库和 bs4 库爬取广轻新闻和信息技术学院的新闻,保存方式为每条新闻保存到一个文件中,文件名以递增数字为文件名,后缀为.txt。 首先,您需要安装 requests 和 beautifulsoup4 库。在终端或命令行中输入以下命令可安装: ``` pip install requests pip install beautifulsoup4 ``` 接下来,您可以使用以下代码爬取广轻新闻: ```python import requests from bs4 import BeautifulSoup import os # 设置请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 设置保存路径 save_path = './gdqy' # 创建保存路径 if not os.path.exists(save_path): os.makedirs(save_path) # 爬取广轻新闻 url = 'http://www.gdqy.edu.cn/' response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') news_list = soup.find_all('li', class_='news_li') # 遍历新闻列表,保存每条新闻到文件 for i, news in enumerate(news_list): # 获取新闻标题和链接 title = news.find('a').text.strip() link = url + news.find('a')['href'] # 发送请求,获取新闻内容 news_response = requests.get(link, headers=headers) news_soup = BeautifulSoup(news_response.content, 'html.parser') news_content = news_soup.find('div', class_='news_content').text.strip() # 保存新闻到文件中 filename = '{}/{}.txt'.format(save_path, i+1) with open(filename, 'w', encoding='utf-8') as f: f.write(link + '\n') f.write(title + '\n') f.write(news_content) ``` 请注意爬取时要设置请求头,否则会被网站拦截。此外,还需要创建保存路径,遍历新闻列表并保存每条新闻到文件中。 接下来,您可以使用以下代码爬取信息技术学院的新闻: ```python import requests from bs4 import BeautifulSoup import os # 设置请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 设置保存路径 save_path = './gdqy' # 创建保存路径 if not os.path.exists(save_path): os.makedirs(save_path) # 爬取信息技术学院的新闻 url = 'http://xxjs.gdqy.edu.cn/info/iList.jsp?cat_id=10026' response = requests.get(url, headers=headers) soup = BeautifulSoup(response.content, 'html.parser') news_list = soup.find_all('li', class_='news_li') # 遍历新闻列表,保存每条新闻到文件 for i, news in enumerate(news_list): # 获取新闻标题和链接 title = news.find('a').text.strip() link = 'http://xxjs.gdqy.edu.cn' + news.find('a')['href'] # 发送请求,获取新闻内容 news_response = requests.get(link, headers=headers) news_soup = BeautifulSoup(news_response.content, 'html.parser') news_content = news_soup.find('div', class_='news_content').text.strip() # 保存新闻到文件中 filename = '{}/{}.txt'.format(save_path, i+1) with open(filename, 'w', encoding='utf-8') as f: f.write(link + '\n') f.write(title + '\n') f.write(news_content) ``` 与广轻新闻的爬取类似,需要设置请求头、创建保存路径、遍历新闻列表并保存每条新闻到文件中。 最后,您可以在终端或命令行中运行以上代码,爬取新闻并保存到 gdqy 目录下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值