Python之爬蟲 etree和XPath實戰

2021-08-26 09:22:22 字數 3518 閱讀 3054

下面**是在**上找到的乙個例子,空閒的時候可以自己除錯。 

# -*- coding:utf-8 -*-

""" 爬蟲 創業邦 創業公司資訊爬取

網頁url = ''

爬取頁面中的創業公司,融資階段,創業領域,成立時間和創業公司的鏈結資訊。

使用到requests, json, codecs, lxml等庫

requests用於訪問頁面,獲取頁面的源**

josn庫用於寫入json檔案儲存到本地

codecs庫用於讀寫檔案時編碼問題

lxml用於解析網頁源**,獲取資訊

"""import requests

import json

import codecs

from lxml import etree

class chuangyebang:

def __init__(self):

pass

def get_html(self, url):

""" get_html

得到網頁源**,返回unicode格式

@param: url

@return: r.text """

headers =

r = requests.get(url, headers=headers)

print r.status_code

return r.text

def get_company_data(self, text):

""" get_company_data

得到網頁資訊

eg:

@param: text 網頁的源**unicode格式源**

@return: list 乙個頁面所有的公司資訊 列表中每乙個元素為存入資訊的字典

"""html = etree.html(text) # 解析網頁

company_name_list = html.xpath(

'//td[@class="table-company-tit"]/a/span/text()'

)# 得到帶有class"table-company-tit"...屬性的td標籤下的a標籤下的span標籤的內容,返回為乙個列表

print company_name_list # get companyname list

print len(company_name_list)

company_url_list = html.xpath(

'//td[@class="table-company-tit"]/a/@href'

)"""

得到帶有..屬性的td標籤下的a標籤中hred的內容

為乙個url

"""print company_url_list

stage_list = html.xpath('//td[@class="table-stage"]/@data-stage')

# 同上 不解釋了 得到stage

company_stage_list =

for company_stage in stage_list:

company_stage = company_stage.strip(',') if company_stage else none

print company_stage_list # get stage list

print len(company_stage_list)

company_type_list = html.xpath('//td[@class="table-type"]')

type_list =

for company_type in company_type_list:

company_type = company_type.xpath('./a/text()')[0] \

if company_type.xpath('./a/text()') else none

print type_list

print len(type_list)

company_time_list = html.xpath('//td[@class="table-time"]/text()')

print company_time_list

print len(company_time_list)

"""遍歷每個列表,取出列表對應的元素,組成我們需要的字典

"""ret_company_list =

for i in range(20):

single_company = {}

single_company['companyurl'] = company_url_list[i]

single_company['companyname'] = company_name_list[i]

single_company['type'] = type_list[i]

single_company['stage'] = company_stage_list[i]

single_company['time'] = company_time_list[i]

return ret_company_list

def write_in_json(self, data):

""" write_in_json

寫入json檔案

codecs # 用於編碼,同一用utf-8格式編碼

json.dumps # 方法用於將字典或者列表轉換成json字串格式,存入json檔案

indent=2 # json檔案中顯示的方法,顯示為2字元的鎖緊

.decode('unicode_escape') # 在json檔案中顯示中文,不會顯示utf-8編碼,方便看。

"""json_data = json.dumps(data, indent=2).decode('unicode_escape')

with codecs.open('./chuangyebang.json', 'w', 'utf-8') as fw:

fw.write(json_data)

class getcompanyinfo:

""" 得到每個公司詳細資訊 """

def __init__(self):

pass

def get_html_text(self, url):

headers = {}

r = requests.get(url, headers=headers)

print r.status_code

return r.text

def get_company_info(self, text):

pass

if __name__ == "__main__":

cyb = chuangyebang()

url = ''

text = cyb.get_html(url)

data = cyb.get_company_data(text)

cyb.write_in_json(data)

Python之lxml模組的etree類的使用

lxml的安裝與etree類的匯入 將html字串轉化為element物件,且elment物件的方法 element物件的xptah方法 安裝方式 在終端cmd下利用pip命令安裝即可 保證網路暢通 pip install lxml element物件是xpath語法的使用物件,element物件可...

Python之lxml模組的etree類的使用

lxml的安裝與etree類的匯入 將html字串轉化為element物件,且elment物件的方法 element物件的xptah方法 安裝方式 在終端cmd下利用pip命令安裝即可 保證網路暢通 pip install lxml element物件是xpath語法的使用物件,element物件可...

Python爬蟲之Cookie和Session

關於cookie和session估計很多程式設計師面試的時候都會被問到,這兩個概念在寫web以及爬蟲中都會涉及,並且兩者可能很多人直接回答也不好說的特別清楚,所以整理這樣一篇文章,也幫助自己加深理解 其實簡單的說就是當使用者通過http協議訪問乙個伺服器的時候,這個伺服器會將一些name value...