Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用正则表达式提取twitter文本数据中的@user和url链接_Python_Regex_Twitter_Text - Fatal编程技术网

Python使用正则表达式提取twitter文本数据中的@user和url链接

Python使用正则表达式提取twitter文本数据中的@user和url链接,python,regex,twitter,text,Python,Regex,Twitter,Text,有一个列表字符串twitter文本数据,例如,下面的数据(实际上,有大量的文本,不仅仅是这些数据),我想提取twitter文本中@和url链接之后的所有用户名,例如:galaxy5univ和url链接 tweet_text = ['@galaxy5univ I like you', 'RT @BestOfGalaxies: Let's sit under the stars ...', '@jonghyun__bot .........((thanks)', 'RT

有一个列表字符串twitter文本数据,例如,下面的数据(实际上,有大量的文本,不仅仅是这些数据),我想提取twitter文本中@和url链接之后的所有用户名,例如:galaxy5univ和url链接

   tweet_text = ['@galaxy5univ I like you',
    'RT @BestOfGalaxies: Let's sit under the stars ...',
    '@jonghyun__bot .........((thanks)',
    'RT @yosizo: thanks.ddddd <https://yahoo.com>',
    'RT @LDH_3_yui: #fam, ccccc https://msn.news.com']
通过在大量twitter数据中测试代码,我发现我的url和name两种模式都是错误的(尽管在一些twitter文本数据中是正确的)。你们有没有关于从twitter文本中提取名称和url的文档或链接,以处理大型twitter数据


如果您有关于从twitter数据中提取姓名和url的建议,请告诉我,谢谢

如果用户名不包含特殊字符,则可以使用:

@([\w]+)

请参见注意,您的
pn=re.compile(r'@(\S+)
regex将捕获
@
之后的任何1+非空白字符

要排除匹配的
,您需要将速记
\S
类转换为
[^\S]
否定字符类等效项,并向其添加

pn = re.compile(r'@([^\s:]+)')
现在,它将在第一个
之前停止捕获非空白符号。请参阅

如果需要捕获到最后一个
,则只需在捕获组之后添加
:pn=re.compile(r'@(\S+))

至于URL匹配正则表达式,有一个最适合你

以下是一份:

重新导入
p=re.compile(r'@([^\s:]+))
test_str=“@galaxy5univ I like you\nRT@BestOfGalaxies:让我们坐在星星下\n@jonghyun__bot((谢谢)\nRT@yosizo:谢谢。ddddd\nRT@LDH_3_yui:#fam,ccccchttps://m...content-available-to-author-only...s.com"
印刷品(p.findall(测试)
p2=重新编译(r'(?:http | ftp | https):/(?:[\w-]+(?:(?:\[\w-]+)+)(?:[\w,@?^=%&:/~++-]*[\w@?^=%&/~+-]))
打印(p2.findall(测试)
#=>['galaxy5univ','BestOfGalaxies','jonghyun_uuubot','yosizo','LDH_u3_uYui']
# => ['https://yahoo.com', 'https://msn.news.com']

pn=re.compile(r'@([a-zA-Z0-9+))
感谢您的评论,您知道twitter数据中有大量的姓名数据。有时姓名中包含一些特殊字符,如#%^,而不仅仅是a-zA-Z0-9。在这种情况下,如何解决?只需将其添加到方括号内的字符列表中,但请记住,某些字符需要正确输入即可谢谢你的评论,但我必须在方括号内添加所有字符。如果我不知道@后面的字符,在这种情况下,如何解决它。我希望有有效的方法解决它(删除名称末尾的“:”)。你的意思是在
@
之后获得所有非空白字符,而不是
?你可以使用
r'@([^\s:]+'
现在,我发现我的url和name两种模式都是错误的。你们有关于从twitter文本中提取name和url的文档或链接吗。
@([^\s:]+)有什么问题吗
?URL的正则表达式可以在任何地方找到。这是一个很好的资源。这里有一个SO线程。请参阅。感谢您的热情。例如,一些名称:@t:*d-8:。您知道twitter中的名称有不同的形式。对不起,我从未见过带有空格的用户名。这意味着您需要
@(.*):
,对吗?如果没有,请解释这些用户名所属的模式。如果没有模式,则无法匹配它们。此外,这里还提到了Twitter JS库中使用的正则表达式(该模式与Python兼容)。我真的非常感谢@Wiktor Stribiżew的帮助。我会阅读你提到的文档。你是一个善良的人。谢谢你的评论。我知道我在twitter文本中提取@后的名称和url链接的两种模式是错误的。你知道名称和url链接有很多种形式。如果你有一些关于这方面的文档或链接,请致电我爱我!
pn = re.compile(r'@([^\s:]+)')
import re
p = re.compile(r'@([^\s:]+)')
test_str = "@galaxy5univ I like you\nRT @BestOfGalaxies: Let's sit under the stars ...\n@jonghyun__bot .........((thanks)\nRT @yosizo: thanks.ddddd <https://y...content-available-to-author-only...o.com>\nRT @LDH_3_yui: #fam, ccccc https://m...content-available-to-author-only...s.com"
print(p.findall(test_str)) 
p2 = re.compile(r'(?:http|ftp|https)://(?:[\w_-]+(?:(?:\.[\w_-]+)+))(?:[\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?')
print(p2.findall(test_str))
# => ['galaxy5univ', 'BestOfGalaxies', 'jonghyun__bot', 'yosizo', 'LDH_3_yui']
# => ['https://yahoo.com', 'https://msn.news.com']