Skip to main content
 首页 » 编程设计

python之如何检测强标签并为每个标签添加 "*"

2025年02月15日752php

我在 python 中有这段代码,它对我的​​作用是从 web 延伸。网页文章的文本内容,并保存在不同的文件中。我想知道,如何检测强标签并在每个标签之前或之后添加一个“”。

这是我需要的结果:

 import urllib2 
    import re 
    from bs4 import BeautifulSoup 
    import time 
 
 
    def _remove_attrs(soup): 
        for tag in soup.findAll(True): 
 
            href='' 
            if (tag.has_attr('href')): 
            href=tag.get('href') 
 
            src='' 
            if (tag.has_attr('src')): 
                src=tag.get('src') 
 
            # tag.attrs = None 
        tag.attrs = {} 
         if (href!=''): 
            tag['href']= href 
 
        if (src!=''): 
            tag['src']= src 
 
    return soup 
 
def _remove_empty(soup): 
    return soup 
    for x in soup.find_all(): 
        if len(x.text) == 0: 
            x.extract() 
    return soup 
 
 
    base_url= 'http://www.scavonehnos.com.py/index.php? 
    mact=Vmcs,cntnt01,print,0&cntnt01articleid=' 
 
   for x in range(10,12): 
       n_url=base_url + str(x) 
       print ("#PAGINA: "+n_url) 
       page = urllib2.urlopen(n_url) 
       soup = BeautifulSoup(page, 'html.parser') 
 
       contenido=(soup.div.get_text()) 
 
       file = open('vicentec/prod_'+str(x)+'.txt', 'w') 
       file.write(u' '.strip(contenido).join((contenido)).encode('utf- 
8')) 
       file.close() 
 
 
       time.sleep(5) 

如您所见,我想将星号添加到 <strong>在网络上标记。

请您参考如下方法:

对于那些访问过这个问题的人,这个案例我已经解决了,它仍然存在并且运行良好

import urllib2 
import re 
from bs4 import BeautifulSoup 
import time 
 
 
def _remove_attrs(soup): 
    for tag in soup.findAll(True): 
 
        href='' 
        if (tag.has_attr('href')): 
            href=tag.get('href') 
 
        src='' 
        if (tag.has_attr('src')): 
            src=tag.get('src') 
 
        # tag.attrs = None 
        tag.attrs = {} 
        if (href!=''): 
            tag['href']= href 
 
        if (src!=''): 
            tag['src']= src 
 
    return soup 
 
def _remove_empty(soup): 
    return soup 
    for x in soup.find_all(''): 
        if len(x.text) == 0: 
            x.extract() 
    return soup 
 
base_url= 'http://www.scavonehnos.com.py/index.php?mact=Vmcs,cntnt01,print,0&cntnt01articleid=' 
 
for x in range(10,225): 
    n_url=base_url + str(x) 
    print ("#PAGINA: "+n_url) 
    page = urllib2.urlopen(n_url) 
    soup = BeautifulSoup(page, 'html.parser') 
 
    for strong in soup.select('strong'): 
        strong.replace_with('#'+strong.get_text()) 
 
    contenido=(soup.div.get_text()) 
 
 
    fprod = 'vicentec/prod_'+(str(x))+'.txt' 
    file = open(fprod, "w") 
    file.write(u' '.strip(contenido).join((contenido)).encode('utf-8')) 
    file.close()