Python 爬虫小程序(正则表达式的应用)
生活随笔
收集整理的這篇文章主要介紹了
Python 爬虫小程序(正则表达式的应用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目標:通過正則表達式寫一個爬蟲程序,抓下網頁的所有圖片。思路1. ?獲取網頁源代碼2. ?獲取圖片
3. ?下載圖片
第二步,獲取圖片相關地址(正則匹配)
從取回的源代碼中分析圖片相關URL 的構造,然后通過正則表達式將圖片地址提取出來源文件中圖片的標簽是這樣子的:<img class="BDE_Image" src="http://imgsrc.baidu.com/forum/w%3D580/sign=2e8f3ca53af33a879e6d0012f65d1018/4ece3bc 79f3df8dc2ab63004cd11728b46102899.jpg" width="560" height="400" changedsize="true">要獲取的是http://imgsrc.baidu.com/xxxxxxx.jpg
#!/usr/bin/python import re import urllibdef getHtml(url):html = urllib.urlopen(url)scode = html.read()return scodedef getImage(source):re = r'src="(.*?\.jpg)" width='imgre = re.compile(re)p_w_picpaths = re.findall(imgre,source)return p_w_picpathssource = getHtml('http://tieba.baidu.com/p/1762577651') print getImage(source)轉載于:https://blog.51cto.com/pmghong/1334086
總結
以上是生活随笔為你收集整理的Python 爬虫小程序(正则表达式的应用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Intel AVX-512指令集发飙 性
- 下一篇: python3.2安装tornado