重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
這篇文章主要為大家詳細介紹了簡單的python爬蟲代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
python爬蟲代碼示例分享
一、爬取故事段子:
注:部分代碼無法正常運行,但仍有一定的參考價值。
#encoding=utf-8 import urllib2 import re class neihanba(): def spider(self): ''' 爬蟲的主調度器 ''' isflow=True#判斷是否進行下一頁 page=1 while isflow: url="http://www.neihanpa.com/article/list_5_"+str(page)+".html" html=self.load(url) self.deal(html,page) panduan=raw_input("是否繼續(y/n)!") if panduan=="y": isflow=True page+=1 else: isflow=False def load(self,url): ''' 針對url地址進行全部爬去 :param url: url地址 :return: 返回爬去的內容 ''' header = { "User-Agent": " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" } request = urllib2.Request(url, headers=header) response = urllib2.urlopen(request) html = response.read() return html def deal(self,html,page): ''' 對之前爬去的內容進行正則匹配,匹配出標題和正文內容 :param html:之前爬去的內容 :param page: 正在爬去的頁碼 ''' parrten=re.compile('
","").replace("
","").replace("二、爬取智聯:
#encoding=utf-8 import urllib import urllib2 import re class zhiLian(): def spider(self,position,workPlace): ''' 爬蟲的主調度器 :param position: 職位 :param workPlace: 工作地點 ''' url="http://sou.zhaopin.com/jobs/searchresult.ashx?" url+=urllib.urlencode({"jl":workPlace}) url+="&" url+=urllib.urlencode({"kw":position}) isflow=True#是否進行下一頁的爬去 page=1 while isflow: url+="&"+str(page) html=self.load(url) self.deal1(html,page) panduan = raw_input("是否繼續爬蟲下一頁(y/n)!") if panduan == "y": isflow = True page += 1 else: isflow = False def load(self,url): ''' 針對url地址進行全部爬去 :param url: url地址 :return: 返回爬去的內容 ''' header = { "User-Agent": " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" } request = urllib2.Request(url, headers=header) response = urllib2.urlopen(request) html = response.read() return html def deal1(self,html,page): ''' 對之前爬去的內容進行正則匹配,匹配職位所對應的鏈接 :param html:之前爬去的內容 :param page: 正在爬去的頁碼 ''' parrten=re.compile('.*?',re.S) til=parrten.findall(html)#爬去鏈接 for t in til: self.deal2(t,page) def deal2(self,t,page): ''' 進行二次爬蟲,然后在新的頁面中對公司、薪資、工作經驗進行匹配 :param t: url地址 :param page: 當前匹配的頁數 ''' html=self.load(t)#返回二次爬蟲的內容 parrten1=re.compile('(.*?)\s+.*?',re.S) parrten2=re.compile('
三、爬取貼吧:
#encoding=utf-8 import urllib import urllib2 import re class teiba(): def spider(self,name,startPage,endPage): url="http://tieba.baidu.com/f?ie=utf-8&" url+=urllib.urlencode({"kw":name}) for page in range(startPage,endPage+1): pn=50*(page-1) urlFull=url+"&"+urllib.urlencode({"pn":pn}) html=self.loadPage(url) self.dealPage(html,page) def loadPage(self,url): header={ "User-Agent":" Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" } request=urllib2.Request(url,headers=header) response=urllib2.urlopen(request) html=response.read() return html def dealPage(self,html,page): partten=re.compile(r'(.*?)',re.S) titleList=partten.findall(html) rstr=r'#(.*?)#' for title in titleList: title=re.sub(rstr,"",title) self.writePage(title,page) def writePage(self,context,page): fileName="di"+str(page)+"yehtml.txt" with open(fileName,"a") as file: file.writelines(context+"\n") if __name__ == '__main__': name=raw_input("請輸入貼吧名:") startPage=raw_input("請輸入起始頁:") endPage=raw_input("請輸入終止頁:") t=teiba() t.spider(name,int(startPage),int(endPage))
以上就是簡單的python爬蟲代碼的詳細內容了,看完之后是否有所收獲呢?如果想了解更多相關內容,歡迎來創新互聯建站行業資訊!