python网页数据抓取(Python,抓取网页和只抓网页图片的脚本的相关内容吗)
优采云 发布时间: 2021-10-16 06:44python网页数据抓取(Python,抓取网页和只抓网页图片的脚本的相关内容吗)
想学习使用Python3编写网页抓取脚本和网页图片抓取脚本吗?在本文中,大摩天升将为大家讲解Python3爬取网页脚本的相关知识以及一些代码示例。欢迎阅读和纠正我们。先画重点:Python,抢,一起学起来。
抓取网页内容最基本的代码实现:
#!/usr/bin/env python
from urllib import urlretrieve
def firstNonBlank(lines):
for eachLine in lines:
if not eachLine.strip():
continue
else:
return eachLine
def firstLast(webpage):
f = open(webpage)
lines = f.readlines()
f.close()
print firstNonBlank(lines),
lines.reverse()
print firstNonBlank(lines),
def download(url='http://www',process=firstLast):
try:
retval = urlretrieve(url)[0]
except IOError:
retval = None
if retval:
process(retval)
if __name__ == '__main__':
download()
使用urllib模块实现网页抓图功能:
import urllib.request
import socket
import re
import sys
import os
targetDir = r"H:\pic"
def destFile(path):
if not os.path.isdir(targetDir):
os.mkdir(targetDir)
pos = path.rindex('/')
t = os.path.join(targetDir, path[pos+1:]) #会以/作为分隔
return t
if __name__ == "__main__":
hostname = "http://www.douban.com/"
req = urllib.request.Request(hostname)
webpage = urllib.request.urlopen(req)
contentBytes = webpage.read()
match = re.findall(r'(http:[^\s]*?(jpg|png|gif))', str(contentBytes) )#r'(http:[^\s]*?(jpg|png|gif))'中包含两层圆括号,故有两个分组,
#上面会返回列表,括号中匹配的内容才会出现在列表中
for picname, picType in match:
print(picname)
print(picType)
'''''
输出:
http://img3.douban.com/pics/blank.gif
gif
http://img3.douban.com/icon/g111328-1.jpg
jpg
http://img3.douban.com/pics/blank.gif
gif
http://img3.douban.com/icon/g197523-19.jpg
jpg
http://img3.douban.com/pics/blank.gif
gif
...
'''
相关文章