学习python少不了写爬虫,不仅能以点带面地学习、练习使用python,爬虫本身也是有用且有趣的,大量重复性的下载、统计工作完全可以写一个爬虫程序完成。
用python写爬虫需要python的基础知识、涉及网络的几个模块、正则表达式、文件操作等知识。昨天在网上学习了一下,写了一个爬虫自动下载「糗事百科」里面的图片。源代码如下:
代码如下:
# -*- coding: utf-8 -*-
# 上面那句让代码里支持中文
#---------------------------------------
# 程序:糗百图片爬虫
# 版本:0.1
# 作者:赵伟
# 日期:2013-07-25
# 语言:Python 2.7
# 说明:能设置下载的页数。没有做更多抽象和交互方面的优化。
#---------------------------------------
import urllib2
import urllib
import re
#正则表达式,用来抓取图片的地址
pat = re.compile('
#用来合成网页的URL
nexturl1 = "http://m.qiushibaike.com/imgrank/page/"
nexturl2 = "?s=4582487&slow"
#页数计数
count = 1
#设置抓取的页数
while count
print "Page " + str(count) + "\n"
myurl = nexturl1 + str(count) + nexturl2
myres = urllib2.urlopen(myurl)#抓取网页
mypage = myres.read()#读取网页内容
ucpage = mypage.decode("utf-8") #转码
mat = pat.findall(ucpage)#用正则表达式抓取图片地址
count += 1;
if len(mat):
for item in mat:
print "url: " + item + "\n"
fnp = re.compile('/(\w+\.\w+)$')#下面三行分离出图片文件的名称
fnr = fnp.findall(item)
fname = fnr[0]
urllib.urlretrieve(item, fname)#下载图片
else:
print "no data"
使用方法:新建一个practice文件夹,将源代码保存为qb.py文件,并放在practice文件夹中,在命令行里执行python qb.py,即开始下载图片。可以修改源代码里面的while语句设置下载的页数。

Pythonusesahybridmodelofilationandlostretation : 1) ThePyThoninterPretreCeterCompileSsourcodeIntOplatform-IndependentBecode.

Pythonisbothingretedandcompiled.1) 1) it 'scompiledtobytecodeforportabilityacrossplatforms.2) thebytecodeisthentenningreted, withfordiNamictyTeNgreted, WhithItmayBowerShiledlanguges.

forloopsareusedwhendumberofitessiskNowninadvance, whilewhiloopsareusedwhentheationsdepernationsorarrays.2) whiloopsureatableforscenarioScontiLaspecOndCond

pythonisnotpurelynlogreted; itusesahybrideprophorfbyodecodecompilationandruntime -INGRETATION.1) pythoncompilessourcecodeintobytecode, thepythonVirtualMachine (pvm)

ToconcatenatelistsinpythonwithesameElements, 사용 : 1) OperatorTokeEpduplicates, 2) asettoremovedUplicates, or3) listComperensionForControlOverDuplicates, 각 methodHasDifferentPerferformanCeanDorderImpestications.

PythonisancerpretedLanguage, 비판적 요소를 제시하는 PytherfaceLockelimitationsIncriticalApplications.1) 해석 된 언어와 같은 thePeedBackandbackandrapidProtoTyping.2) CompilledlanguagesLikec/C transformt 해석

useforloopswhhenmerfiterationsiskNownInAdvance 및 WhileLoopSweHeniTesslationsDepoyConditionismet whilEroopsSuitsCenarioswhereTheLoopScenarioswhereTheLoopScenarioswhereTheLoopScenarioswhereTherInatismet, 유용한 광고 인 푸트 gorit


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구