国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

三個python爬蟲項目實例代碼

2024-09-09 19:03:29
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了三個python爬蟲項目實例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

爬取內涵段子:

#encoding=utf-8import urllib2import reclass 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('<li class="piclist/d+">(.*?)</li>',re.S)    titleList=parrten.findall(html)    for title in titleList:      parrten1=re.compile('<a href="/article//d+.html" rel="external nofollow" >(.*)</a>')      ti1=parrten1.findall(title)      parrten2=re.compile('<div class="f18 mb20">(.*?)</div>',re.S)      til2=parrten2.findall(title)      for t in ti1:        tr=t.replace("<b>","").replace("</b>","")        self.writeData(tr,page)      for t in til2:        tr=t.replace("<p>","").replace("</p>","").replace("<br>","").replace("<br />","").replace("&ldquo","/"").replace("&rdquo","/"")        self.writeData(tr,page)  def writeData(self,context,page):    '''    將最終爬去的內容寫入文件中    :param context: 匹配好的內容    :param page: 當前爬去的頁碼數    '''    fileName = "di" + str(page) + "yehtml.txt"    with open(fileName, "a") as file:      file.writelines(context + "/n")if __name__ == '__main__':  n=neihanba()  n.spider()

爬取智聯:

#encoding=utf-8import urllibimport urllib2import reclass 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('<a/s+/s+par="ssidkey=y&ss=/d+&ff=/d+&sg=/w+&so=/d+"/s+href="(.*?)" rel="external nofollow" target="_blank">.*?</a>',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('<a/s+onclick=".*?"/s+href=".*?" rel="external nofollow" /s+target="_blank">(.*?)/s+.*?<img/s+class=".*?"/s+src=".*?"/s+border="/d+"/s+vinfo=".*?"></a>',re.S)    parrten2=re.compile('<li><span>職位月薪:</span><strong>(.*?) <a.*?>.*?</a></strong></li>',re.S)    parrent3=re.compile('<li><span>工作經驗:</span><strong>(.*?)</strong></li>',re.S)    til1=parrten1.findall(html)    til2=parrten2.findall(html)    til3=parrent3.findall(html)    str=""    for t in til1:      t=t.replace('<img title="專屬頁面" src="http://img03.zhaopin.cn/2012/img/jobs/icon.png" border="0" />',"")      str+=t      str+="/t"    for t in til2:      str+=t      str += "/t"    for t in til3:      str+=t    self.writeData(str,page)  def writeData(self,context,page):    '''    將最終爬去的內容寫入文件中    :param context: 匹配好的內容     :param page: 當前爬去的頁碼數    '''    fileName = "di" + str(page) + "yehtml.txt"    with open(fileName, "a") as file:      file.writelines(context + "/n")if __name__ == '__main__':  position=raw_input("請輸入職位:")  workPlace=raw_input("請輸入工作地點:")  z=zhiLian()  z.spider(position,workPlace)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 花莲市| 天津市| 藁城市| 休宁县| 石阡县| 凭祥市| 天水市| 盐城市| 黔西| 鞍山市| 新野县| 广西| 昆明市| 镇安县| 尼玛县| 合川市| 诸城市| 定襄县| 大丰市| 安顺市| 柳江县| 拉孜县| 西峡县| 固始县| 东明县| 精河县| 仙桃市| 景洪市| 和田县| 庆城县| 海淀区| 门头沟区| 榆社县| 融水| 义乌市| 桐梓县| 新巴尔虎左旗| 忻城县| 钟祥市| 东海县| 汕头市|