以爬取“我要愛死你”貴州行政區劃為例,淺析lxml中etree的用法
lxml是python中一個非常強大的解析庫。其中的etree更是常常用來判斷網頁中某一節點是否存在,並獲取相應的文字或屬性。
一、用法詳解
1、匯入etree
import requests
from lxml import etree
2、獲取網頁資訊並生成etree選擇器,即下面中的html
res = requests.get('http://www.baidu.com')
html =etree.HTML(res.text)
3、利用選擇器的xpath()函式,獲取相應的節點
datalist01 = html.xpath('//table//table//a/text()')#獲取當前頁面下的表格下的表格中的a標籤的文字
datalist02 = html.xpath('//table//table//a/@href')#獲取當前頁面下的表格下的表格中的a標籤的href屬性
注:
1) // 雙斜槓 定位根節點,會對全文進行掃描,在文件中選取所有符合條件的內容,以列表的形式返回。
2) / 單斜槓 尋找當前標籤路徑的下一層路徑標籤或者對當前路標籤內容進行操作
3) /text() 獲取當前路徑下的文字內容
4) /@xxxx 提取當前路徑下標籤的屬性值
5) | 可選符 使用|可選取若干個路徑 如//p | //div 即在當前路徑下選取所有符合條件的p標籤和div標籤。
6) . 點 用來選取當前節點
7) .. 雙點 選取當前節點的父節點
二、爬取“我要愛死你”網站程式碼
import requests
import time
from lxml import etree
from xlrd import open_workbook
from xlutils.copy import copy
#鄉鎮------>村
def parseCun(link):
header={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
time.sleep(3)
res = requests.get(link,headers=header)
html =etree.HTML(res.text)
datalist = html.xpath('//table//table//a/text()')
datalist2 = html.xpath('//table//table//a/@href')
n = 5
name = []
code = []
url = []
sjcode = link[33:45]
print(sjcode)
while n < len(datalist):
if n%2 != 0:
name.append(datalist[n])
url.append('https://xingzhengquhua.51240.com'+datalist2[n])
else:
code.append(datalist[n])
n = n+1
saveData(sjcode,name,code)
#區縣------>鄉鎮
def parseQxj(link):
header={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
res = requests.get(link,headers=header)
html =etree.HTML(res.text)
datalist = html.xpath('//table//table//a/text()')
datalist2 = html.xpath('//table//table//a/@href')
n = 4
name = []
code = []
url = []
sjcode = link[33:45]
print(sjcode)
while n < len(datalist):
if n%2 != 1:
name.append(datalist[n])
else:
code.append(datalist[n])
url.append('https://xingzhengquhua.51240.com'+datalist2[n])
n = n+1
i = 0
for u in url:
print(name[i])
sname = []
scode = []
sname.append(name[i])
scode.append(code[i])
saveData(sjcode,sname,scode)
time.sleep(3)
parseCun(u)
del sname[:]
del scode[:]
i = i + 1
#市州---->區縣
def parseSzj(link):
header={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
res = requests.get(link,headers=header)
html =etree.HTML(res.text)
datalist = html.xpath('//table//table//a/text()')
datalist2 = html.xpath('//table//table//a/@href')
n = 3
name = []
code = []
url = []
sjcode = link[33:45]
print(sjcode)
while n < len(datalist):
if n%2 != 0:
name.append(datalist[n])
url.append('https://xingzhengquhua.51240.com'+datalist2[n])
else:
code.append(datalist[n])
n = n + 1
i = 0
for u in url:
print(name[i])
sname = []
scode = []
sname.append(name[i])
scode.append(code[i])
saveData(sjcode,sname,scode)
time.sleep(3)
parseQxj(u)
del sname[:]
del scode[:]
i = i + 1
#解析省級 --->市州
def parseGzs(link):
header={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36"
}
res = requests.get(link,headers=header)
html =etree.HTML(res.text)
datalist = html.xpath('//table//table//a/text()')
datalist2 = html.xpath('//table//table//a/@href')
n = 2
name = []
code = []
url = []
sjcode = link[33:45]
print(sjcode)
while n < len(datalist):
if n%2 != 1:
name.append(datalist[n])
else:
code.append(datalist[n])
url.append('https://xingzhengquhua.51240.com'+datalist2[n])
n = n + 1
i = 0
print(url)
for u in url:
sname = []
scode = []
print(2)
sname.append(name[i])
scode.append(code[i])
saveData(sjcode,sname,scode)
parseSzj(u)
del sname[:]
del scode[:]
i = i + 1
#儲存資料
def saveData(sjcoede,name,bjcode):
rexcel = open_workbook("./行政區劃.xls")
rows = rexcel.sheets()[0].nrows
excel = copy(rexcel)
table = excel.get_sheet(0)
row = rows
i = 0
for code in bjcode:
table.write(row, 0, sjcoede)
table.write(row, 1, name[i])
table.write(row, 2, bjcode[i])
row += 1
i = i +1
excel.save("./行政區劃.xls")
#主函式
def main(url):
parseGzs(url)
if __name__ == '__main__':
url = 'https://xingzhengquhua.51240.com/520000000000__xingzhengquhua/'
main(url)