从微信公众号[俄语摆渡]获取中俄翻译的内容

#引入需要的模块
from requests import request
from bs4 import BeautifulSoup
import time
import re

#需要用到的自定义函数
#get Href from web
def acquirehref(href):
    return request('GET', href).text

#输入微信文章页面链接
htmltext=acquirehref('https://mp.weixin.qq.com/s/nffP8boYAQGkhr39zzj0VQ')

#获取页面信息
BeautS=BeautifulSoup(htmltext,'html.parser')
BeautS.encoding = 'utf-8'

#获取文章标题news_title
news_title=BeautS.findAll('h1',{'class':'rich_media_title'})
for each in news_title:
  file_name = each.text.strip()

#获取文章内容并保存在与文章标题同名的文件里
itemsnews=BeautS.findAll('div',{'class':'rich_media_content'})
with open(f'{file_name}.txt', 'w', encoding='utf-8') as f:
  for each in itemsnews:
    #print(tag.text.strip())
    f.writelines(each.text.strip()+'\n')

你可能感兴趣的:(Python,python)