在抓取时删除标题中的一些文本

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

我目前正在尝试搜索youtube播放列表。废料工作,但我想只得到标题的一部分。

例如 :

  • 视频标题是: 如果我们吃了这些知识怎么办? | Idriss Aberkane | TEDxPanthéonSorbonne“
  • 通过刮,我只想得到: “如果我们吃了这些知识怎么办?”

我想删除|之后的所有字符

可能吗?

python string scrapy
4个回答
0
投票
import re

p = re.compile("(.*?) \|.*")
m = p.search('Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne')

这给出了你想要的字符串:

m[1]

0
投票

如果你确定“|”你可以在每个标题中写出这样的字符

string title = "test title | about anything";
string result ="";
if(title.indexOf("|") > -1)
    result = title.substring(0, test.indexOf("|"));

0
投票

如果要在第一次出现“|”时删除所有内容你可以写下面的代码:

scrap_result = 'Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne' # this is the scrap result of the title you get you can user str() to be precise so you only get string is a title.
scrap_result = scrap_result[:scrap_result.find("|")] # this will give you result before the first occurrence of '|' but it includes trailing space at the end if you want to remove it use scrap_result.strip() 

-1
投票

是的,你有两个选择:切片

String = 'Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne'
String = String[-1:-x]

更换:

String = String.replace(' | Idriss Aberkane | TEDxPanthéonSorbonne', '')
© www.soinside.com 2019 - 2024. All rights reserved.