import re
import urllib.request
start_url = 'http://49.232.208.237/zs/'
def get_source(url):
"""
獲取網(wǎng)頁源代碼胃惜。
:param url: 網(wǎng)址
:return: 網(wǎng)頁源代碼
"""
html = urllib.request.urlopen(url) # 獲取URL頁面源代碼
return html.read().decode('UTF-8') # 網(wǎng)站編碼
def getPageList(html):
"""
獲取頁碼列表和頁碼鏈接
:param html: 頁面源代碼
:return: 每頁鏈接和頁標(biāo)題
"""
pageListLink = []
toc_block = re.findall('<div>(.*?)</div>', html, re.S)[0] # 提取所有頁列表
pageTitle = re.findall('.html">(.*?)</a>', html, re.S) # 提取頁標(biāo)題
urls = re.findall('href="(.*?)"', toc_block, re.S) # 提取頁面鏈接
for url in urls:
pageListLink.append(start_url + url) # 組合鏈接存入列表
return pageTitle, pageListLink
def getJobInto(html):
"""
或缺每頁的信息列表
:param html: 頁面源代碼
:return: 列表信息
"""
jobs = []
JobInto = re.findall('<div data-v-6c88a070="" class="jobP-info">(.*?)</a></section>', html, re.S) # 匹配所有工作信息
for job in JobInto: # 遍歷每一個(gè)工作,提取工作的詳細(xì)信息
into=[] # 提取工作的信息存入此列表
title = re.search('class="job-name fl">(.*?)</div>', job, re.S).group(1) # 提取標(biāo)題
salary = re.search('class="fl">(.*?)</div>', job, re.S).group(1) # 提取薪資
city = re.search('class="ads">(.*?)</span>', job, re.S).group(1) # 提取城市
company = re.search('class="comp-name fl">(.*?)</div>', job, re.S).group(1) # 提取公司
time = re.search('class="time fr">(.*?)</div>', job, re.S).group(1) # 提取時(shí)間
into.append(title) # 將每個(gè)信息加入到列表
into.append(salary)
into.append(city)
into.append(company)
into.append(time)
jobs.append(into) # 將整理出來的工作信息加入到j(luò)obs列表
return jobs # 每個(gè)工作的信息都以列表形式存入到j(luò)obs列表里,返回該jobs列表
def save(allJob, title):
"""
將每一章保存到本地适滓。
:param allJob: 工作列表
:param title: 頁面標(biāo)題
:return: None
"""
with open('c:\\Users\\ShacoZ\\Desktop\\' + title + '.txt', 'w', encoding='utf-8') as f:
for jobs in allJob: # 遍歷所有工作信息盯另,寫入到文件
f.write("標(biāo)題:"+jobs[0]+'\n')
f.write("薪水:"+jobs[1]+'\n')
f.write("城市:"+jobs[2]+'\n')
f.write("公司:"+jobs[3]+'\n')
f.write("發(fā)布時(shí)間:"+jobs[4]+'\n')
f.write('--------------------------------------------------------------------')
f.write('\n')
if __name__ == '__main__':
page = get_source(start_url) # 獲取主頁源代碼
title, pages = getPageList(page) # 獲取頁面標(biāo)題和頁面鏈接
for (job,tit) in zip(pages,title): # 遍歷鏈接和標(biāo)題
alljob = getJobInto(get_source(job)) # 將鏈接的頁面源代碼傳給獲取工作頁面的函數(shù),并返回所有工作列表
save(alljob, tit) # 將整理出來的工作列表傳給寫入文件函數(shù)
import re
import urllib.request
start_url = 'http://49.232.208.237/zd/'
def get_source(url):
"""
獲取網(wǎng)頁源代碼逼裆。
:param url: 網(wǎng)址
:return: 網(wǎng)頁源代碼
"""
html = urllib.request.urlopen(url) # 獲取URL頁面源代碼
return html.read().decode('UTF-8') # 編碼網(wǎng)頁
def getPageList(html):
"""
獲取頁碼列表和頁碼鏈接
:param html: 目錄頁源代碼
:return: 每頁鏈接
"""
pageNumberLink = []
pageLink = re.findall('class="listhref" href="(.*?)">', html, re.S) # 提取所有頁面鏈接
pageTitle = re.findall('.html">(.*?)</a>', html, re.S) # 頁碼文字
for url in pageLink:
pageNumberLink.append(start_url + url) # 將鏈接組合
return pageTitle, pageNumberLink # 返回鏈接列表和頁面標(biāo)題
def getBlogInto(html):
"""
或缺每頁的信息列表
:param html: 正文源代碼
:return: 列表信息
"""
blogs = []
blogInto = re.findall('item project-item">(.*?)</div>\s*</div>', html, re.S) # 獲取頁面所有文章列表
for blog in blogInto: # 遍歷文章列表蓝仲,提取文章信息
into=[]
if re.search('<span class="project-name">(.*?)</span>', blog, re.S): # 判斷是否能匹配到,匹配不到則設(shè)置為空串
protect = re.search('<span class="project-name">(.*?)</span>', blog, re.S).group(1) # 匹配到項(xiàng)目名信息(標(biāo)題1)
else:
protect = ''
if re.search('project-title">(.*?)</span>', blog, re.S):
title = re.search('project-title">(.*?)</span>', blog, re.S).group(1) # 標(biāo)題2
else:
title = ''
if re.search('data-tooltip="國產(chǎn)">(.*?)</div>', blog, re.S):
domestic = re.search('data-tooltip="國產(chǎn)">(.*?)</div>', blog, re.S).group(1) # 是否是國產(chǎn)
else:
domestic = ''
if re.search('line-clamp">(.*?)</p>', blog, re.S):
text = re.search('line-clamp">(.*?)</p>', blog, re.S).group(1) # 匹配正文
else:
text = ''
if re.search('<div class="item">(.*?)</div>', blog, re.S):
collection = re.search('<div class="item">(.*?)</div>', blog, re.S).group(1) # 收藏?cái)?shù)
else:
collection = ''
if re.search('target="_blank">(.*?)</a>', blog, re.S):
comment = re.search('target="_blank">(.*?)</a>', blog, re.S).group(1) # 評(píng)論
else:
comment = ''
if re.search('更新于 (.*)',blogInto[8],re.S):
time = re.search('更新于 (.*)',blogInto[8],re.S).group(1) # 更新時(shí)間
else:
time = ''
into.append(protect) # 將匹配的信息加入到信息列表
into.append(title)
into.append(domestic)
into.append(text)
into.append(collection)
into.append(comment)
into.append(time)
blogs.append(into) # 將單個(gè)文章信息列表加入到總列表里
return blogs # 返回所有文章信息
def save(Blogs, title):
"""
將每一章保存到本地泡态。
:param Blogs: 文章列表
:param title: 頁碼標(biāo)題
:return: None
"""
with open('c:\\Users\\ShacoZ\\Desktop\\' + title + '.txt', 'w', encoding='utf-8') as f:
for Blog in Blogs:
f.write("標(biāo)題:"+Blog[0])
f.write(Blog[1]+'\n')
if Blog[2]!='': # 如果存在國產(chǎn)字段則寫入國產(chǎn)搂漠,否則不寫入
f.write("國產(chǎn):"+Blog[2]+'\n')
f.write("正文:"+Blog[3]+'\n')
f.write(Blog[4]+'\n')
f.write(Blog[5] + '\n')
f.write("更新時(shí)間:" + Blog[6] + '\n')
f.write('--------------------------------------------------------------------')
f.write('\n')
if __name__ == '__main__':
page = get_source(start_url) # 獲取主鏈接源代碼
title, pages = getPageList(page) # 匹配頁碼列表和列表鏈接
for (job,tit) in zip(pages,title):
allBlog = getBlogInto(get_source(job)) # 遍歷鏈接和頁碼標(biāo)題,提取每頁的所有文章信息
save(allBlog, tit) # 將文章信息列表傳給寫入文件函數(shù)