简体   繁体   English

如何在python中的字符串之前打印一定数量的字符

[英]how to print a certain amount of characters before a string in python

hi i have a python script that is going to a website and searching for strings inside of certain tags and printing it. 您好,我有一个Python脚本,该脚本将转到一个网站并在某些标签内搜索字符串并进行打印。 my screen will look like this after it prints it - textidontwant textiwanthere.com how can i search for the .com and print a number of characters before it to only get the textiwanthere.com to show up instead of all of it. 我的屏幕在打印后会看起来像这样-textidontwant textiwanthere.com如何搜索.com并在打印之前打印许多字符,以便仅显示textiwanthere.com而不是全部显示。 here is my code - 这是我的代码-

import urllib.request
import re
import os

url = "http://www.throwawaymail.com/"

request = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
sourcecode = urllib.request.urlopen(request).read()
output = sourcecode.decode("utf-8")

findemail = re.findall('>(.*?)</span>', str(output))

print(findemail)

os.system("pause")

i want to search "findemail" for it i want to print the phamepracl@throwam.com but its different everytime but the length is the same this is what my console says - 我想搜索“ findemail”,我想打印phamepracl@throwam.com,但每次都不同,但是长度相同,这就是我的控制台所说的-

['Toggle navigation', '', '', '', '', 'phamepracl@throwam.com'] [“切换导航”,“,”,“,”,“ phamepracl@throwam.com”]

Just print the last entry of the list 只需打印列表的最后一项

print(findemail)[-1]

You could also assign this value to findmail if you don't want the other stuff 如果您不想其他东西,也可以将此值分配给findmail

findemail = re.findall('>(.*?)</span>', str(output))[-1]

This worked for me: 这对我有用:

import urllib.request
import re
import os

url = "http://www.throwawaymail.com/"

request = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
sourcecode = urllib.request.urlopen(request).read()
output = sourcecode.decode("utf-8")

findemail = re.findall('>(.*?)</span>', str(output))

print(findemail[-1])

This is my solution: 这是我的解决方案:

for i in findemail:
    if i.find('.com')>=0:
        print(i)

Output: 输出:

hudininona@throwam.com

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

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