引言
基于Pyside2开发一个可视化程序,抓取readhub网站每日早报消息,显示在程序主界面上,当用户用鼠标选中标题时,点击显示按钮控件,显示该条消息的内容在第二QLabel控件上。
准备工作
·· 1、导入使用的库
import requests
import re
from bs4 import BeautifulSoup as bs
from PySide2 import QtCore, QtGui, QtWidgets
注:前提须安装好第三方库(pip install 库名)
查看目标网页源代码
抓取主要标题
def parse_title(self):
self.parse_date()
res_1 = self.response_url('https://readhub.cn/daily')
title = res_1.find_all('a', {'class': {'Daily_item__RWo9q'}})
self.new_title.clear()
self.new_content.clear()
for i in title:
data = re.findall(r'<a class=".*?" href=".*?" target=".*?">(.*?)</a>', str(i))
result = ''.join(data)
self.new_title.append(result)
self.parse_content(str(i))
抓取每一条标题对应的内容
def parse_content(self, tmp_info):
res_2 = self.response_content_url(tmp_info)
data = res_2.find_all('div', {'class': {'style_bp-pure__4Mjnd'}})
for j in data:
content = re.findall(r'<div class=".*?">(.*?)<div class=".*?" title=".*?"></div></div>', str(j))
result = ''.join(content)
self.new_content.append(result)
运行效果
具体代码
链接:https://pan.baidu.com/s/1jWfQKxR9MUmi-gO3B7xahg
提取码:aaaa
今天就先到这里啦,如果对你有帮助的,赶紧收藏下来吧!觉得代码哪里有问题或者有建议的,都可以打在评论上,我会留意的,互相学习,互相探讨,你我皆黑马。