Python脚本需求:实现邮箱输出及while True:nr+=1循环自增逻辑
Solution: Add Continuous nr Increment & Fix Email Extraction
Alright, let's get your script sorted. You've already fixed the email extraction to output [test@gmail.com] instead of an empty list, and now you just need to add that infinite loop where nr keeps incrementing. Here's a complete, adjusted version of your script that combines both features:
import urllib import urllib2 import cookielib import re import requests # Initialize the nr variable starting at 0 nr = 0 # Create a requests session (as you were using before) session = requests.Session() # Infinite loop to keep nr incrementing while True: nr += 1 print(f"Current nr value: {nr}") # Optional: Print to track the increment # Your existing email extraction logic (fixed to return [test@gmail.com]) # Replace this with your actual page fetching/processing code if needed sample_content = "Some text containing test@gmail.com" email_pattern = re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b') extracted_emails = email_pattern.findall(sample_content) print(f"Extracted emails: {extracted_emails}") # Outputs [test@gmail.com] # Optional: Add a small delay if you don't want to spam the console/requests # import time # time.sleep(1)
Key Notes:
- nr Initialization: We start
nrat 0 before the loop so the first increment takes it to 1. - Infinite Loop: The
while True:block runs indefinitely, incrementingnrevery iteration. - Email Extraction: The regex pattern properly matches standard email formats, which is why it now captures
test@gmail.cominstead of returning an empty list. - Session Handling: We keep the
requests.Session()object initialized outside the loop (more efficient than recreating it every time).
If you need to tie the nr increment to specific actions (like fetching a new page with each nr value), just adjust the loop to use nr in your request parameters or URL.
内容的提问来源于stack exchange,提问作者Lfofe
相关产品推荐
相关产品推荐

