提取网页上的突出显示文本

2024-04-26 15:07:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有任何方式可以从网页上的段落中提取突出显示的文本。在

经过长时间的搜索,我遇到了这个模块https://python-docx.readthedocs.io/en/latest/ 但这是为了文件。在

例如,假设我们有以下段落:

“Stack Overflow是一个私有网站,Stack Exchange Network的旗舰网站,由Jeff Atwood和Joel Spolsky于2008年创建。它的创建是为了成为早期问答网站(如专家交流)的更开放的替代方案。2008年4月,Atwood的热门编程博客codinghorry的读者投票选出了这个网站的名字。 它的特色是关于计算机编程的广泛主题的问答。”

现在在上面的段落中,假设粗体字符串是我高亮显示的单词,我想提取并输出突出显示的单词。有没有办法我可以在网页上做到这一点。在

因此输出应为: 私人网站;专家交流;话题广泛。在


Tags: 模块https文本网页stack网站编程方式
3条回答

我认为这个解决方案更适用于您正在寻找的:

const req = require('tinyreq');

req('http://www.treepad.com/docs/tpp/manual/documents/127A901E40BA449B3C4359B720246BA3B2E67362.html', (err, body) => {
    if (err) { return console.log(err); }
    body.split('<body')[1].split('<span').map(textBold => {
        if(textBold.includes('background-color:')){
            console.log(textBold.split('>')[1].split('</SPAN')[0]);
            console.log('────────────────────');
        }
    });
});

output of treepad Highlighting text example link

^{pr2}$

我要做的就是使用tinyreq,在尸体上搜索标签。这可能很有用:

const req = require('tinyreq');

const start = '<mark>'; const end = '</mark>';
// const start = '<b>'; const end = '</b>';
// const start = '<strong>'; const end = '</strong>';

req('https://en.wikipedia.org/wiki/Language_code', (err, body) => {
    if (err) { return console.log(err); }
    body.split('<body')[1].split(start).map(textBold => {
        if(textBold.includes(end)){
            console.log(textBold.split(end)[0]);
            console.log('────────────────────');
        }
    });
});

您只需使用bs4即可完成此操作。首先确保您已经安装了bs4和requests,如果您想安装它们,只需运行这两个命令

pip install requests
pip install bs4

然后你必须写一个像这样的python脚本

^{pr2}$

相关问题 更多 >