requests和lxml實(shí)現(xiàn)爬蟲(chóng)的方法
如下所示:
# requests模塊來(lái)請(qǐng)求頁(yè)面
# lxml模塊的html構(gòu)建selector選擇器(格式化響應(yīng)response)
# from lxml import html
# import requests
# response = requests.get(url).content
# selector = html.formatstring(response)
# hrefs = selector.xpath('/html/body//div[@class='feed-item _j_feed_item']/a/@href')
# 以u(píng)rl = 'https://www.mafengwo.cn/gonglve/ziyouxing/2033.html'為例子
# python 2.7 import requests from lxml import html import os
# 獲取首頁(yè)中子頁(yè)的url鏈接 def get_page_urls(url): response = requests.get(url).content # 通過(guò)lxml的html來(lái)構(gòu)建選擇器 selector = html.fromstring(response) urls = [] for i in selector.xpath("/html/body//div[@class='feed-item _j_feed_item']/a/@href"): urls.append(i) return urls
# get title from a child's html(div[@class='title']) def get_page_a_title(url): '''url is ziyouxing's a@href''' response = requests.get(url).content selector = html.fromstring(response) # get xpath by chrome's tool --> /html/body//div[@class='title']/text() a_title = selector.xpath("/html/body//div[@class='title']/text()") return a_title
# 獲取頁(yè)面選擇器(通過(guò)lxml的html構(gòu)建) def get_selector(url): response = requests.get(url).content selector = html.fromstring(response) return selector
# 通過(guò)chrome的開(kāi)發(fā)者工具分析html頁(yè)面結(jié)構(gòu)后發(fā)現(xiàn),我們需要獲取的文本內(nèi)容主要顯示在div[@class='l-topic']和div[@class='p-section']中
# 獲取所需的文本內(nèi)容 def get_page_content(selector): # /html/body/div[2]/div[2]/div[1]/div[@class='l-topic']/p/text() page_title = selector.xpath("http://div[@class='l-topic']/p/text()") # /html/body/div[2]/div[2]/div[1]/div[2]/div[15]/div[@class='p-section']/text() page_content = selector.xpath("http://div[@class='p-section']/text()") return page_title,page_content
# 獲取頁(yè)面中的圖片url地址 def get_image_urls(selector): imagesrcs = selector.xpath("http://img[@class='_j_lazyload']/@src") return imagesrcs
# 獲取圖片的標(biāo)題 def get_image_title(selector, num) # num 是從2開(kāi)始的 url = "/html/body/div[2]/div[2]/div[1]/div[2]/div["+num+"]/span[@class='img-an']/text()" if selector.xpath(url) is not None: image_title = selector.xpath(url) else: image_title = "map"+str(num) # 沒(méi)有就起一個(gè) return image_title
# 下載圖片 def downloadimages(selector,number): '''number是用來(lái)計(jì)數(shù)的''' urls = get_image_urls() num = 2 amount = len(urls) for url in urls: image_title = get_image_title(selector, num) filename = "/home/WorkSpace/tour/words/result"+number+"/+"image_title+".jpg" if not os.path.exists(filename): os.makedirs(filename) print('downloading %s image %s' %(number, image_title)) with open(filename, 'wb') as f: f.write(requests.get(url).content) num += 1 print "已經(jīng)下載了%s張圖" %num
# 入口,啟動(dòng)并把獲取的數(shù)據(jù)存入文件中 if __name__ =='__main__': url = 'https://www.mafengwo.cn/gonglve/ziyouxing/2033.html' urls = get_page_urls(url) # turn to get response from html number = 1 for i in urls: selector = get_selector(i) # download images downloadimages(selector,number) # get text and write into a file page_title, page_content = get_page_content(selector) result = page_title+'\n'+page_content+'\n\n' path = "/home/WorkSpace/tour/words/result"+num+"/" if not os.path.exists(filename): os.makedirs(filename) filename = path + "num"+".txt" with open(filename,'wb') as f: f.write(result) print result
到此就結(jié)束了該爬蟲(chóng),爬取頁(yè)面前一定要認(rèn)真分析html結(jié)構(gòu),有些頁(yè)面是由js生成,該頁(yè)面比較簡(jiǎn)單,沒(méi)涉及到j(luò)s的處理,日后的隨筆中會(huì)有相關(guān)分享
以上這篇requests和lxml實(shí)現(xiàn)爬蟲(chóng)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
django從請(qǐng)求到響應(yīng)的過(guò)程深入講解
這篇文章主要給大家介紹了關(guān)于django從請(qǐng)求到響應(yīng)的過(guò)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Python實(shí)現(xiàn)url長(zhǎng)短鏈接的轉(zhuǎn)換
短鏈接,通俗來(lái)說(shuō),就是將長(zhǎng)的URL網(wǎng)址,通過(guò)程序計(jì)算等方式,轉(zhuǎn)換為簡(jiǎn)短的網(wǎng)址字符串。本文將用Python語(yǔ)言實(shí)現(xiàn)這一效果,需要的可以參考一下2022-11-11Python自動(dòng)重新加載模塊詳解(autoreload module)
這篇文章主要介紹了Python自動(dòng)重新加載模塊詳解(autoreload module),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Python?time時(shí)間格式化和設(shè)置時(shí)區(qū)實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Python?time時(shí)間格式化和設(shè)置時(shí)區(qū)實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值2023-02-02基于OpenCV和Gradio實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別詳解
這篇文章主要為大家詳細(xì)介紹了如何基于OpenCV和Gradio實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04python利用百度AI實(shí)現(xiàn)文字識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python利用百度AI實(shí)現(xiàn)文字識(shí)別,主要涉及通用文字識(shí)別、網(wǎng)絡(luò)圖片文字識(shí)別、身份證識(shí)別等文字識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Keras框架中的epoch、bacth、batch size、iteration使用介紹
這篇文章主要介紹了Keras框架中的epoch、bacth、batch size、iteration使用介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06