0
import urllib
import os
import re
import smtplib
from email.mime.text import MIMEText
import time

usedEmails = []
num = 0

while True:
    u = urllib.urlopen("http://otvet.mail.ru/")
    page = u.read()

    emailPattern = r"[0-9a-zA-Z_\-\.]+@[0-9a-z-A-Z\.]+\.[a-zA-Z]+"
    compliedpattern = re.compile(emailPattern)

    for address in compliedpattern.findall(page):
        if not address in usedEmails:

Как сделать, чтобы он собирал мыло в простой текстовый документ?

2 ответа 2

2

Вот скрипт, проходит все категории и все страницы...Вчера тоже эту статью из хакера прочитал, решил с питоном ознакомиться, вот дополнил к уже существующему...

#!/usr/bin/env python

import urllib
import re

f = open('mails.txt','ab')

url = 'http://otvet.mail.ru'
u = urllib.urlopen(url)
page = u.read()
urlPattern = r'<li><a href="(.*?)"  title="'
urls = re.findall(r'<li><a href="(.*?)"  title="',page, re.DOTALL|re.MULTILINE)
for (categoryUrl) in urls:

    x = 1
    while x <= 50:
        url = "http://otvet.mail.ru" + categoryUrl + "open/?pg=" + str(x)
        print(url)
        u = urllib.urlopen(url)
        page = u.read()
        emailPattern = "[0-9a-zA-Z_\-\.]+@[0-9a-z-A-Z\.]+ru"
        compiledPattern = re.compile(emailPattern)
        unic = uniq = list(set(compiledPattern.findall(page)))
        unic.remove("--Rating@Mail.ru")
        unic.remove("Rating@Mail.ru")
        x += 1

        for (address) in unic:
            print(address)
            f.write(address + '\r\n')
f.close

Одна строка явно лишняя, ищи сам.

0

не шарю в питоне

Перед циклом (for address in ...) открывать файл на запись

Соответственно, как раз в конце вашего кода записывать в него адрес + "\n".

Погугление дало примерно такое:

f = open(r'addr.txt', 'w')
for address in compliedpattern.findall(page):
  if not address in usedEmails:
    f.write(address . "\n")
f.close()
1
  • пишет что имя urlopen ненайдено что делать?
    – afagorn
    2 окт 2011 в 6:26

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.