Python3.4实现简易微博图片抓取爬虫

拾柒
拾柒
发布时间:2026-10-13 | 3人浏览过
原创

本文介绍基于Python3.4编写的简易爬虫程序,利用urllib和re模块抓取指定微博用户的头像图片。代码包含设置User-Agent、处理Cookie及正则表达式匹配等关键步骤,适合初学者学习网络请求与数据解析基础。

本示例展示了如何使用Python3.4编写一个简单的网页抓取程序,供读者参考学习。

import urllib.request
import urllib.parse
import re
import urllib.request,urllib.parse,http.cookiejar
import time
def getHtml(url):
  cj=http.cookiejar.CookieJar()
  opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'),('Cookie','4564564564564564565646540')]
  urllib.request.install_opener(opener)
  page = urllib.request.urlopen(url)
  html = page.read()
  return html
#print ( html)
#html = getHtml("http://weibo.com/")
def getimg(html):
  html = html.decode('utf-8')
  reg='"screen_name":"(.*?)"'
  imgre = re.compile(reg)
  src=re.findall(imgre,html)
  return src
#print ("",getimg(html))
uid=['2808675432','3888405676','2628551531','2808587400']
for a in list(uid):
  print (getimg(getHtml("http://weibo.com/"+a)))
  time.sleep(1)

如需了解更多Python相关知识,可参考本站的其他专题,包括《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》以及《Python文件与目录操作技巧汇总》。

希望上述内容能对您的Python编程实践提供有益帮助。

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表