【问题标题】:Python 2.7 - Can only concatenate list (not "str") to listPython 2.7 - 只能将列表(不是“str”)连接到列表
【发布时间】:2018-02-08 18:52:07
【问题描述】:

我的代码:

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
nr=85365
login_data = urllib.urlencode({'username' : username, 'password' : password})
nrs = urllib.urlencode({'id' : nr})
opener.open('http://ddddd/takelogin.php', login_data)
while True:
    resp = opener.open('http://ddd/userdetails.php?id=' + str(nr))
    s=resp.read()
    ss=re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",s)
    file = open("cookies.txt","a")
    file.write(ss +"\n")
    #time.sleep(10)
    print nr
    nr+=1

我想从输出中保存电子邮件。 我有这个错误,我使用 python 2.7

    file.write(ss +"\n")
TypeError: can only concatenate list (not "str") to list

【问题讨论】:

  • ss 作为re.findall() 的结果显然是一个列表。你期待什么?此外,您的正则表达式严重不足。
  • 我该如何改造它?
  • 你期待什么?

标签: python string python-2.7


【解决方案1】:

如果您希望每行一封电子邮件,您需要这个

with open("cookies.txt","a") as file:
    while True:
        resp = opener.open('http://ddd/userdetails.php?id=' + str(nr))
        s=resp.read()
        ss=re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",s)
        for email in ss:
            file.write(email +"\n")
        #time.sleep(10)
        print nr
        nr+=1

我建议你限制nr的值以避免无限循环

【讨论】:

    猜你喜欢
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2020-06-02
    相关资源
    最近更新 更多