1

我的代码有效,但我想测试多个帐户。我想我必须在我的程序运行之前清除所有的 cookie,但是我尝试了很多东西,但它们都没有奏效。

这是我的代码:

import mechanize
from bs4 import BeautifulSoup
import urllib2 
import cookielib
import csv

fic_csv = raw_input("name : ")
delimiter = raw_input("délimiter : ")

def test ():

    with open(fic_csv+".csv", "r")  as csvfile:
        readCSV = csv.reader(csvfile, delimiter=':')
        pws = []
        mails = []

        i = 0
        while (i<2):
            for row in readCSV:
                mail = row[0]
                pw = row[1]

                pws.append(pw)
                mails.append(mail)
                mail_site = mails[i]
                pw_site = pws[i]



                cj = cookielib.CookieJar()
                br = mechanize.Browser()
                br.set_cookiejar(cj)
                br.open("url")
                br.select_form(nr=0)
                br.form['login'] = mail_site
                br.form['password'] = pw_site
                res = br.submit()
                html = res.read() #read and store the result html page


                list_srch =["tag1","tag2"]
                ii = 0
                while (ii < 2):

                    br2 = mechanize.Browser()
                    br2.set_cookiejar(cj)
                    br2.open("url")
                    br2.select_form(nr=0)
                    br2.form['sq'] = list_srch[ii]
                    res2 = br2.submit()
                    html2 = res2.read() #read and store the result html page
                    soup = BeautifulSoup(html2, 'lxml')
                    table1 = soup.findAll("table",{ "width" : "100%" })[13] # 
                    tr1 = table1.findAll('tr')[3] 
                    table2 = tr1.findAll("table",{ "width" : "100%" })[0]
                    tr2 = table2.findAll('tr')[1] 
                    tr3 = tr2.findAll('td')[5] 
                    resultat = tr3.string
                    print resultat 
                    fic_dest= str(("%s.csv" % list_srch[ii]))
                    with open (fic_dest, "w") as fdest: 
                        writer = csv.writer(fdest)
                        writer.writerow( (mail, pw, resultat) )
                        print 'over'
                    ii += 1
            i += 1

test()
4

0 回答 0