在网页中查找字符串而不将其保存在文件中?

发布于 2024-12-12 01:57:58 字数 917 浏览 5 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心激荡 2024-12-19 01:57:58

这应该会让你前进。该脚本使用正则表达式打印网页中的所有链接:

import re, urllib
page = urllib.urlopen("http://sebsauvage.net/index.html").read()
urls = re.findall('href=[\'"]?([^\'" >]+)',page)
for url in urls:
    print url

This should get you forward. This script prints all links from the web-page using your regular expression:

import re, urllib
page = urllib.urlopen("http://sebsauvage.net/index.html").read()
urls = re.findall('href=[\'"]?([^\'" >]+)',page)
for url in urls:
    print url
孤千羽 2024-12-19 01:57:58

更新了 Lycha 对 python3 的答案

import re, urllib.request
page = urllib.request.urlopen("http://sebsauvage.net/index.html").read().decode('utf-8')
urls = re.findall('href=[\'"]?([^\'" >]+)', page)
for url in urls:
    print(url)

Updated Lycha's answer for python3

import re, urllib.request
page = urllib.request.urlopen("http://sebsauvage.net/index.html").read().decode('utf-8')
urls = re.findall('href=[\'"]?([^\'" >]+)', page)
for url in urls:
    print(url)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文