python urllib2 问题
我正在尝试从网址打印一些信息,但如果找到某个文本,我想跳过打印,我有:
import urllib2
url_number = 1
url_number_str = number
a = 1
while a != 10:
f = urllib2.urlopen('http://example.com/?=' + str(url_number_str)
f_contents = f.read()
if f_contents != '{"Response":"Parse Error"}':
print f_contents
a += 1
url_number_str += 1
所以 {"Response":"Parse Error"} 是我想要找到以避免打印的文本f.read() 并加载下一个 url(编号 2)
I am trying to print some info from an url, but I want to skip the print if a certain text if found, I have:
import urllib2
url_number = 1
url_number_str = number
a = 1
while a != 10:
f = urllib2.urlopen('http://example.com/?=' + str(url_number_str)
f_contents = f.read()
if f_contents != '{"Response":"Parse Error"}':
print f_contents
a += 1
url_number_str += 1
so {"Response":"Parse Error"} is the text that I want to find to avoid printing f.read() and load the NEXT url (Number 2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管您的问题有点不清楚,但请尝试以下操作:
这会循环网页中的每一行,并在
'{"Response":"Parse Error"}'
处停止。编辑:没关系,这可能就是您想要的:
这将打印整个网页,除非它是
'{"Response":"Parse Error"}'
。Although your question is a bit unclear, try this:
This loops over every line in the webpage, and stops at
'{"Response":"Parse Error"}'
.Edit: Nevermind, this is probably what you want:
This will print the entire webpage, unless it is
'{"Response":"Parse Error"}'
.read
读取一个数据块。该块的实际大小可能大于'{"Response":"Parse Error"}'
。因此,您应该使用 RE 或
strstr
之类的方式在读取的数据中搜索字符串(请参阅@harpyon 的答案)。read
reads a block of data. The actual size of this block is more than probably greater than the'{"Response":"Parse Error"}'
.So you should search the string within the read data (see @harpyon's answer), using RE or
strstr
like.我认为这就是您想要的:
尽管如果您不想获得相同的页面 100 次,您可能忘记在 URL 中添加
a
。I think this is what you want:
Although if you're not wanting to get the same page 100 times, you might have forgotten to add
a
into the URL.