Python2.7网页爬虫实战,BeautifulSoup抓取网页图片和页面重要信息

本文介绍了一个使用Python2.7和BeautifulSoup编写的网页爬虫示例,该示例能抓取网站上的图片并保存到指定文件夹,同时将页面关键信息存储为TXT文件。项目源码可在Gitee上找到。尽管该程序可正常运行,但长时间运行后可能会遇到未知问题。
摘要由CSDN通过智能技术生成

自己用python+BeautifulSoup做的一个小工具,获取网站的图片存入指定文件夹,获取页面重要信息存入txt文件。代码如下:

__author__ = 'wangzg'
# -*- coding:utf-8 -*-
import urllib
import urllib2
import re
import cookielib
import bs4
import sys
import os

reload(sys)
sys.setdefaultencoding( "utf-8" )

f = open('chunguowangInfo.txt', 'w') 
imageDir = 'chunguowangImgs'
imagesf = open('chunguowangImgsInfo.txt', 'w') 
if os.path.exists(imageDir):
    message = 'OK, the "%s" file exists.'
else:
    #message = "Sorry, I cannot find the "%s" file.
	os.makedirs(imageDir)
pwd = os.getcwd()

user_agent ='"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36"'  
headers = { 'User-Agent' : user_agent }  

'''cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookie
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不安分的猿人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值