简体   繁体   English

Python请求连接错误

[英]Python Requests Connection Error

So when I have this code it work perfect: 因此,当我有此代码时,它可以完美工作:

import requests
import re

def clean(toclean):
    m = re.findall("'(.*?)\'", str(toclean))
    rdy = ''.join([item.rstrip('\n') for item in m])
    return pretty(rdy)

def pretty(pret):
    m = re.findall('UA-[0-9]+-[0-9]+', str(pret))
    rdy = ''.join([item.rstrip('\n') for item in m])
    return rdy

r = requests.get('http://editinginsider.com')

m = re.findall('UA-[0-9]+-[0-9]+', r.text)

print clean(m)

But when I try to iterate over a list in a text file line by line I get this name or services not know error 但是,当我尝试逐行遍历文本文件中的列表时,得到此名称或服务不知道错误

import requests
import re

def clean(toclean):
    m = re.findall("'(.*?)\'", str(toclean))
    rdy = ''.join([item.rstrip('\n') for item in m])
    return pretty(rdy)

def pretty(pret):
    m = re.findall('UA-[0-9]+-[0-9]+', str(pret))
    rdy = ''.join([item.rstrip('\n') for item in m])
    return rdy

f = open( "domains.txt", "r" )

for line in f:
    r = requests.get(line, timeout=7)
    m = re.findall('UA-[0-9]+-[0-9]+', r.text)
    print clean(m)

f.close()

So what is the deal? 那怎么办? I have tried sleeping, timeouts, upping the max connection attempts and it fails. 我尝试了睡眠,超时,增加最大连接尝试次数,但失败了。

My bet is something dumb. 我的赌注是愚蠢的。

This is caused most likely caused by a '\\n' character at the end of the line from the domain.txt file. 这很可能是由于domain.txt文件行尾的'\\ n'字符引起的。 The following might work: 以下可能有效:

r = requests.get(line.strip(), timeout=7)

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

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