【问题标题】:issue extracting html page's string using bs4使用 bs4 提取 html 页面的字符串的问题
【发布时间】:2016-03-27 22:34:42
【问题描述】:

我正在编写一个程序来查找歌词,程序快要完成了,但是我对 bs4 数据类型有一点问题, 我的问题是如何从行尾的歌词变量中提取纯文本?

import re
import requests
import bs4
from urllib import unquote

def getLink(fileName):
    webFileName = unquote(fileName)
    page = requests.get("http://songmeanings.com/query/?query="+str(webFileName)+"&type=songtitles")    
    match = re.search('songmeanings\.com\/[^image].*?\/"',page.content)
    if match:
        Mached = str("http://"+match.group())
        return(Mached[:-1:]) # this line used to remove a " at the end of line
    else:
        return(1)       

def getText(link):    
    page = requests.get(str(link))          
    soup = bs4.BeautifulSoup(page.content ,"lxml")     
    return(soup)        

Soup = getText(getLink("paranoid android"))
lyric = Soup.findAll(attrs={"lyric-box"})
print (lyric)

这里是outout:

[\n\t\t\t\t\t\t请你停止噪音,
\n我正在尝试休息一下
\n我脑海中所有未出生的鸡的声音\n那是什么?
\n那是什么?
\n
\n当我为王时,你会第一个碰壁
\n你的意见根本不重要
\n那是什么?
\n那是什么?
\n
\n野心让你看起来很丑
\n踢和尖叫的Gucci小猪
\n你不记得了
\ n你不记得了
\n你为什么不记得我的名字?
\n砍掉他的头,伙计
\n砍掉他的头,伙计
\n你为什么不记得我的名字?
\n我猜他是这样
\n
\n下雨,下雨
\n来吧,下雨了
\n从高处
\n从高处,高度
\n下雨,下雨
\n快下雨,下雨
\n从很高的高度
\n从很高的高度,高度,
\n下雨,下雨
\n给我下雨
\n
\n就是这样,先生
\n你要走了
\n猪皮的噼啪声
\n尘土和尖叫
\n雅皮士网络
\n恐慌,呕吐
\n恐慌,呕吐
\n上帝爱他的孩子,
\n上帝爱他的孩子,是的!

\n编辑歌词\n编辑 Wiki\n添加视频\n
]

【问题讨论】:

  • 请提供您的回溯
  • 不确定您要做什么,但在获取 url 的正则表达式中,这部分 [^image] 是一个字符类,而不是一个短语。如果您将其更改为(?!image),那么它将是一个短语。除此之外,提取文本是什么意思?你的意思是那些\n 是文字,还是它们只是调试换行控制代码的文本表示?
  • by [^image] 我想要不包含这样的行的行:songmeanings.com/image/PATH/TO/AN/IMAGE.png 我的意思是提取纯文本一些功能将 html 文件转换为纯文本,当然我知道我可以用 sed 和 awk 来做,但我真的很喜欢在我的 python 程序中做它而不使用正则表达式
  • @kato 您需要一行代码来从标签中提取文本。请参阅下面的帖子。

标签: python regex bs4


【解决方案1】:

添加以下代码行:

lyric = ''.join([tag.text for tag in lyric])

之后

lyric = Soup.findAll(attrs={"lyric-box"})

你会得到类似的输出

                        Please could you stop the noise,
I'm trying to get some rest
From all the unborn chicken voices in my head
What's that?
What's that?

When I am king, you will be first against the wall
With your opinion which is of no consequence at all
What's that?
What's that?

...

【讨论】:

    【解决方案2】:

    首先通过执行stringvar[1:-1] 修剪前导和尾随[],然后在每一行调用linevar.strip(),这将删除所有空格。

    【讨论】:

      【解决方案3】:

      对于喜欢这个想法的人,经过一些小改动,我的代码最终看起来像这样:)

      import re
      import pycurl
      import bs4
      from urllib import unquote
      from StringIO import StringIO
      
      
      def getLink(fileName):
          fileName = unquote(fileName)
          baseAddres = "https://songmeanings.com/query/?query="
          linkToPage = str(baseAddres)+str(fileName)+str("&type=songtitles")
          
          buffer = StringIO()
          page = pycurl.Curl()
          page.setopt(page.URL,linkToPage)
          page.setopt(page.WRITEDATA,buffer)
          page.perform()
          page.close()
          
          pageSTR = buffer.getvalue()
          
          soup = bs4.BeautifulSoup(pageSTR,"lxml")  
          
          tab_content = str(soup.find_all(attrs={"tab-content"}))    
          pattern = r'\"\/\/songmeanings.com\/.+?\"'
          links = re.findall(pattern,tab_content)
          
          """returns first mached item without double quote
          at the beginning and at the end of the string"""
          return("http:"+links[0][1:-1:])
      
          
      def getText(linkToSong):
          
          buffer = StringIO()
          page = pycurl.Curl()
          page.setopt(page.URL,linkToSong)
          page.setopt(page.WRITEDATA,buffer)
          page.perform()
          page.close()
          
          pageSTR = buffer.getvalue()
          
          soup = bs4.BeautifulSoup(pageSTR,"lxml")  
          
          lyric_box = soup.find_all(attrs={"lyric-box"})
          lyric_boxSTR = ''.join([tag.text for tag in lyric_box])
          return(lyric_boxSTR)
          
          
      link = getLink("Anarchy In The U.K")
      text = getText(link)
      print(text)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多