在网页中查找字符串而不将其保存在文件中?
我是Python新手,我有一些问题!
def extractdownloadurl(url):
uresponse = urllib2.urlopen(url) #open url
contents = uresponse.readlines() #readlines from url file
fo = open("test.html","w") #open test.html
for line in contents:
fo.write(line)#write lines from url file to text file
fo.close()#close text file
cadena = os.system('more test.html | grep uploads | grep zip >> cadena.html')
f = open("cadena.html","r")
text = f.read()
f.close()
match = re.search(r'href=[\'"]?([^\'" >]+)', text)
if match:
cadena=match.group(0)
texto = cadena[6:]
os.system('rm test.html')
os.system('rm cadena.html')
return texto
这是我的功能,用于下载网页并根据某些条件获取一个网址。有用。但我想应用一种比将网络保存在文件中更有效的方法。我想制作类似于 grep 的东西,而不保存和读取文件(这真的很慢)。以及其他将 url 复制到字符串的更快方法。
请编写代码来查找内容中的 url,而不将内容保存到文件中。
我知道有很多问题,但如果您能回答所有问题,我将不胜感激。
I'm new with Python and I have some questions!!
def extractdownloadurl(url):
uresponse = urllib2.urlopen(url) #open url
contents = uresponse.readlines() #readlines from url file
fo = open("test.html","w") #open test.html
for line in contents:
fo.write(line)#write lines from url file to text file
fo.close()#close text file
cadena = os.system('more test.html | grep uploads | grep zip >> cadena.html')
f = open("cadena.html","r")
text = f.read()
f.close()
match = re.search(r'href=[\'"]?([^\'" >]+)', text)
if match:
cadena=match.group(0)
texto = cadena[6:]
os.system('rm test.html')
os.system('rm cadena.html')
return texto
This is my function to download a webpage and take one url following some conditions. It works. But I want to apply a more efficient way than saving the web on a file. I want to make something similar to a grep without save and read files (It's really slow). And other faster way of copy the url to a string.
Please write the code to look for a url inside contents without saving contents into a file.
I know that there are a lot of questions but I would be very grateful if you answer all of then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该会让你前进。该脚本使用正则表达式打印网页中的所有链接:
This should get you forward. This script prints all links from the web-page using your regular expression:
更新了 Lycha 对 python3 的答案
Updated Lycha's answer for python3