• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python数据爬下来保存在哪里

python 搞java代码 3年前 (2022-05-21) 37次浏览 已收录 0个评论

python数据爬下来保存在本地,一般是文件或数据库中,但是文件形式相比要更加简单,如果只是自己写爬虫玩,可以用文件形式来保存数据。

#coding=utf-8
<a href="https://www.gaodaima.com/tag/import" title="查看更多关于import的文章" target="_blank">import</a> <a href="https://www.gaodaima.com/tag/url" title="查看更多关于url的文章" target="_blank">url</a>lib.request
import re
import os
  
'''
Urllib 模块提供了<a href="https://www.gaodaima.com/tag/%e8%af%bb%e5%8f%96" title="查看更多关于读取的文章" target="_blank">读取</a>web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据
urlopen 方法用来打开一个url
read方法 用于读取Url上的数据
'''
  
def getHtml(url):
    page = urllib.request.urlopen(url);
    html = page.read();
    return html;
  
def getImg(html):
    imglist = re.findall('img src="(http.*?)"',html
    return imglist
  
html = getHtml("https://www.zhihu.com/question/34378366").decode("utf-8");
imagesUrl = getImg(html);
  
if os.path.exists("D:/imags") == False:
    os.mkdir("D:/imags");
     
count = 0;
for url in imagesUrl:
    print(url)
    if(url.find('.') != -1):
        name = url[url.find('.',len(url) - 5):];
        bytes = urllib.request.urlopen(url);
        f = open("D:/imags/"+str(count)+name, 'wb');
        f.write(bytes.read());
        f.flush();
        f.close();
        count+=1

www#gaodaima.com来源gaodai$ma#com搞$$代**码网搞代码

经测试,基本功能还是可以实现的。花的较多的时间就是正则匹配哪里,因为自己对正则表达式也不是非常熟悉。所以还是花了点时间。

更多相关学习推荐,敬请访问python教程栏目~

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python数据爬下来保存在哪里
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址