DDR爱好者之家 Design By 杰米

这篇文章主要介绍了三个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('<li class="piclist\d+">(.*"/article/\d+.html" rel="external nofollow" >(.*)</a>')
      ti1=parrten1.findall(title)
      parrten2=re.compile('<div class="f18 mb20">(.*"<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-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('<a\s+style="font-weight:\s+bold"\s+par="ssidkey=y&amp;ss=\d+&amp;ff=\d+&amp;sg=\w+&amp;so=\d+"\s+href="(.*" rel="external nofollow" target="_blank">.*".*"\s+href=".*" rel="external nofollow" \s+target="_blank">(.*".*"\s+src="/UploadFiles/2021-04-08/.*">

爬取贴吧:

#encoding=utf-8
import urllib
import urllib2

import re


class teiba():
  def spider(self,name,startPage,endPage):
    url="http://tieba.baidu.com/f"
    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'<a\s+rel="noreferrer"\s+href="/p/\d+" rel="external nofollow" \s+title=".*"\s+target="_blank" class="j_th_tit\s+">(.*"topic-tag"\s+data-name=".*">#(.*"",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))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米