简体   繁体   English

用于删除标签的字符串操作python(网页抓取)

[英]string manipulation python for removing tags (webscraping)

n =  '\n            \n                \n                \n                    £10.00 (14%)\n                \n            \n            \n        '
for ietms in n:
    n2 = n.replace('\n', '')
    n3 = n2.replace(' ', '')
    n4 = n3.split('(')
    n5 = n4[1].replace(')', '')

f = [n4[0], n5]
print(f)

so i currently have this to remove the \\n tags and spaces from the the n variable but i feel as if there is a way to do this in a better optimied manner and was wondering if anyone could help, thank you.所以我目前有这个来从 n 变量中删除 \\n 标签和空格,但我觉得好像有一种方法可以以更好的优化方式做到这一点,并且想知道是否有人可以提供帮助,谢谢。

In Python str has a method called strip() which removes whitespace characters and special characters like \\n and \\r before and after text.在 Python 中 str 有一个名为strip()的方法,它可以删除文本前后的空白字符和特殊字符,如 \\n 和 \\r。

So you could simplify your program to be:因此,您可以将程序简化为:

n =  '\n            \n                \n                \n                    £10.00 (14%)\n                \n            \n            \n        '
print(n.strip())

Which would then output: '£10.00 (14%)'然后输出: '£10.00 (14%)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM