在这里,您可以看到我的代码段.从3天开始它不再起作用了.我的python在Ubuntu 10.04.4 LTS下运行.Python版本是2.6.5.
#!/usr/bin/env python
import urllib2 as ur
...
webpage = []
site = "http://www.gametracker.com/server_info/94.250.218.247:25200/top_players/"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = ur.Request(site , headers=hdr)
data = ur.urlopen(req)
for line in data:
line = line.split(",")
webpage.append(line)
...
Run Code Online (Sandbox Code Playgroud)
这里返回的Error-msg
Traceback (most recent call last):
File "read_top5.py", line 21, in <module>
data = ur.urlopen(req)
File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.6/urllib2.py", line 397, in open
response = meth(req, response)
File "/usr/lib/python2.6/urllib2.py", line 510, in http_response
'http', request, …Run Code Online (Sandbox Code Playgroud) 我正在编写Python代码来扩展从Twitter获取的缩短的URL.我已经获取了所有URL并将它们存储在由换行符分隔的文本文件中.
目前我正在使用:
response = urllib2.urlopen(url)
return response.url
Run Code Online (Sandbox Code Playgroud)
扩大它们.
但是这种urlopen()方法在扩展URL方面似乎并不是很快.
我有大约540万个网址.有没有更快的方法来使用Python扩展它们?
我正在使用以下代码从网站获取数据:
time_out = 4
def tryconnect(turl, timer=time_out, retries=10):
urlopener = None
sitefound = 1
tried = 0
while (sitefound != 0) and tried < retries:
try:
urlopener = urllib2.urlopen(turl, None, timer)
sitefound = 0
except urllib2.URLError:
tried += 1
if urlopener: return urlopener
else: return None
Run Code Online (Sandbox Code Playgroud)
[...]
urlopener = tryconnect('www.example.com')
if not urlopener:
return None
try:
for line in urlopener:
do stuff
except httplib.IncompleteRead:
print 'incomplete'
return None
except socket.timeout:
print 'socket'
return None
return stuff
Run Code Online (Sandbox Code Playgroud)
有没有办法我可以处理所有这些异常而不必每次都有这么多的样板代码?
谢谢!
我正在尝试使用urllib2.urlopen(实际上,我正在使用mechanize,但这是mechanize调用的方法)获取页面当我获取页面时,我得到的回复不完整; 页面被截断.但是,如果我访问页面的非HTTPS版本,我将获得完整的页面.
我在Arch Linux(3.5.4-1-ARCH x86_64)上.我运行openssl 1.0.1c.在我拥有的另一台Arch Linux机器上会出现此问题,但在使用Python 3(3.3.0)时则不会.
这个问题似乎与urllib2没有检索整个HTTP响应有关.
我在唯一允许我使用urllib2(Py I/O)的在线Python解释器上测试它,它按预期工作.这是代码:
import urllib2
u = urllib2.urlopen('https://wa151.avayalive.com/WAAdminPanel/login.aspx?ReturnUrl=%2fWAAdminPanel%2fprivate%2fHome.aspx')
print u.read()[-100:]
Run Code Online (Sandbox Code Playgroud)
最后一行应包含通常的内容</body></html>.
当我试穿urllib.urlretrieve我的机器时,我得到:
ContentTooShortError: retrieval incomplete: got only 11365 out of 13805 bytes
Run Code Online (Sandbox Code Playgroud)
我无法测试urlretrieve在线解释器,因为它不会让用户写入临时文件.晚上,我将尝试从我的机器上获取URL,但是从不同的位置.
我将使用wikitionary转储来进行POS标记.不知何故,它在下载时卡住了.这是我的代码:
import nltk
from urllib import urlopen
from collections import Counter
import gzip
url = 'http://dumps.wikimedia.org/enwiktionary/latest/enwiktionary-latest-all-titles-in-ns0.gz'
fStream = gzip.open(urlopen(url).read(), 'rb')
dictFile = fStream.read()
fStream.close()
text = nltk.Text(word.lower() for word in dictFile())
tokens = nltk.word_tokenize(text)
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Traceback (most recent call last):
File "~/dir1/dir1/wikt.py", line 15, in <module>
fStream = gzip.open(urlopen(url).read(), 'rb')
File "/usr/lib/python2.7/gzip.py", line 34, in open
return GzipFile(filename, mode, compresslevel)
File "/usr/lib/python2.7/gzip.py", line 89, in __init__
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
TypeError: file() argument 1 must be …Run Code Online (Sandbox Code Playgroud) Python新手来了。
我使用 eventlet 和 urllib2 发出大量异步 http 请求。在我的文件顶部有
import eventlet
import urllib
from eventlet.green import urllib2
Run Code Online (Sandbox Code Playgroud)
然后我发出了很多异步 http 请求,并通过这一行成功:
conn = urllib2.urlopen(signed_url, None)
Run Code Online (Sandbox Code Playgroud)
突然间,我收到这个错误:
URLError: <urlopen error [Errno 8] nodename nor servname provided, or not known>
Run Code Online (Sandbox Code Playgroud)
这个错误发生在同一个 urllib2.urlopen 行上,这很奇怪,因为它之前成功过很多次。另外,当我打印signed_url 然后将其粘贴到浏览器时,我会得到正确的响应,因此该url 的格式正确。
我已经浏览过帖子,但找不到合适的调试策略。从概念上讲,什么可能导致此错误?您建议我如何修复它?
我正在使用Python 2.7.6。
谢谢。
我正在使用rest api发送推送通知.文件在这里. 我正在使用金字塔并使用芹菜安排这些推送通知.
这是我的代码示例:
result = urllib2.urlopen(urlRequest, headers={
"X-Parse-Application-Id": settings["parse.application.id"],
"X-Parse-REST-API-Key": settings["parse.restapi.key"],
"Content-Type": "application/json"
})
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/push', json.dumps(data), )
result = json.loads(connection.getresponse().read())
Run Code Online (Sandbox Code Playgroud)
但是芹菜记录了这个错误:
2015-08-18 16:39:45,092 INFO [celery.worker.strategy][MainThread] Received task: app_v1_1.tasks.push_notification[877906d8-1ea7-4b1f-8a54-aa61bffb40e8]
2015-08-18 16:39:45,094 ERROR [celery.worker.job][MainThread] Task app_v1_1.tasks.push_notification[877906d8-1ea7-4b1f-8a54-aa61bffb40e8] raised unexpected: TypeError("urlopen() got an unexpected keyword argument 'headers'",)
Traceback (most recent call last):
File "/home/apnistreet/work/ve/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/comp/work/ve/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/home/comp/work/site/code/apnistreet_v1_1/tasks.py", line …Run Code Online (Sandbox Code Playgroud)