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

问题描述 投票:1回答:3

我想知道是否有从网页上的段落中提取突出显示的文本。

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

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

“Stack Overflow是一个私人拥有的网站,是Stack交换网络的旗舰网站,由Jeff Atwood和Joel Spolsky于2008年创建。它被创建为比以前的问答网站(如Experts-Exchange)更开放的替代方案。该网站的名称是在2008年4月由Atwood的热门节目博客Coding Horror的读者投票选出的。它包含有关计算机编程广泛主题的问题和答案“

现在在上面的段落中,我们可以说大胆的单词是我突出显示的单词,我想提取加上输出突出显示的单词。有没有办法可以在网页上执行此操作。

所以输出应该是:私有网站;专家交流;广泛的主题。

javascript python web-scraping python-requests
3个回答
0
投票

我认为这个解决方案可以更好地应用于您所寻找的内容:

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

white against a dark blue
────────────────────
background
────────────────────
black against a gray background
────────────────────

0
投票

我会做的是使用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('────────────────────');
        }
    });
});

0
投票

你可以用bs4简单地做到这一点。首先确保已经安装了bs4和请求,如果要安装它们,只需运行这两个命令即可

pip install requests
pip install bs4

那么你必须写一个像这样的python脚本

from bs4 import BeautifulSoup
import requests

page_url = 'http://127.0.0.1:1234'
source_code = requests.get(page_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, features="lxml")
for bold in soup.findAll('b'):
    print(bold.contents)
© www.soinside.com 2019 - 2024. All rights reserved.