美化标签外的文本

2024-04-27 12:54:28 发布

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

我想从《宋飞》的每一集中抓住克莱默的台词:

http://www.imsdb.com/TV/Seinfeld.html

我已经把剧集名字的列表放到了一个我标为“剧集”的文件中-列表.txt在

我现在试着只分析KRAMER之后的行,但是它们似乎在标记之外,这就是我遇到的问题。请参阅此处-->;http://www.imsdb.com/transcripts/Seinfeld-Good-News,-Bad-News.html

下面是我尝试使用beauthoulsoup运行的代码。任何线索都将不胜感激。同时,任何未经请求的建议在此征集哈哈。如果你看到我所做的任何让你觉得笨拙或粗野的代码,我很乐意得到反馈。在

干杯!在

from BeautifulSoup import BeautifulSoup
import requests

text = open ("episode-list.txt","r")


for line in text.readlines():
    url = "http://www.imsdb.com/transcripts/Seinfeld-" + line.strip('\n').replace(" ", "-") + ".html"
    r = requests.get(url)
    soup = BeautifulSoup(r.content)
    for tag in soup:
            print soup.findAll('???')

Tags: 代码importtxtcomhttp列表htmlwww
1条回答
网友
1楼 · 发布于 2024-04-27 12:54:28

下面是一个代码片段,作为您开始的参考。。。在

import re
from bs4 import BeautifulSoup

html = """
<b>                             KRAMER
</b>               (enters) Are you up?

<b>               
</b><b>                             JERRY
</b>               (To Kramer) Yeah...(in the phone) Yeah, 
               people do move! Have you ever seen the 
               big trucks out on the street? Yeah, 
               no problem (hangs up the phone).
<b> 
</b><b>               
</b><b>                             KRAMER
</b>               Boy, the Mets blew it tonight, huh?
"""

soup = BeautifulSoup(html, 'html.parser')
for kramer in soup.find_all('b', text=re.compile("\s+KRAMER\s+")):
    print kramer.next_sibling.strip()

输出将是。。。在

^{pr2}$

相关问题 更多 >