requests和lxml實現(xiàn)爬蟲的方法
如下所示:
# requests模塊來請求頁面
# 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')
# 以url = 'https://www.mafengwo.cn/gonglve/ziyouxing/2033.html'為例子
# python 2.7 import requests from lxml import html import os
# 獲取首頁中子頁的url鏈接
def get_page_urls(url):
response = requests.get(url).content
# 通過lxml的html來構(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
# 獲取頁面選擇器(通過lxml的html構(gòu)建) def get_selector(url): response = requests.get(url).content selector = html.fromstring(response) return selector
# 通過chrome的開發(fā)者工具分析html頁面結(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
# 獲取頁面中的圖片url地址
def get_image_urls(selector):
imagesrcs = selector.xpath("http://img[@class='_j_lazyload']/@src")
return imagesrcs
# 獲取圖片的標題
def get_image_title(selector, num)
# num 是從2開始的
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) # 沒有就起一個
return image_title
# 下載圖片
def downloadimages(selector,number):
'''number是用來計數(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
# 入口,啟動并把獲取的數(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é)束了該爬蟲,爬取頁面前一定要認真分析html結(jié)構(gòu),有些頁面是由js生成,該頁面比較簡單,沒涉及到j(luò)s的處理,日后的隨筆中會有相關(guān)分享
以上這篇requests和lxml實現(xiàn)爬蟲的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python Elasticsearch索引建立和數(shù)據(jù)的上傳詳解
在本篇文章里小編給大家整理的是關(guān)于基于python的Elasticsearch索引的建立和數(shù)據(jù)的上傳的知識點內(nèi)容,需要的朋友們參考下。2019-08-08
Python?Melt函數(shù)將寬格式的數(shù)據(jù)表轉(zhuǎn)換為長格式
在數(shù)據(jù)處理和清洗中,melt函數(shù)是Pandas庫中一個強大而靈活的工具,它的主要功能是將寬格式的數(shù)據(jù)表轉(zhuǎn)換為長格式,從而更方便進行分析和可視化,本文將深入探討melt函數(shù)的用法、參數(shù)解析以及實際應(yīng)用場景2023-12-12
解決python通過cx_Oracle模塊連接Oracle亂碼的問題
今天小編就為大家分享一篇解決python通過cx_Oracle模塊連接Oracle亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法詳解
這篇文章主要介紹了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的方法,較為詳細的分析了VPS CENTOS 上配置python,mysql,nginx,uwsgi,django的具體步驟、相關(guān)命令與操作注意事項,需要的朋友可以參考下2019-07-07
django開發(fā)post接口簡單案例,獲取參數(shù)值的方法
今天小編就為大家分享一篇django開發(fā)post接口簡單案例,獲取參數(shù)值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

