当前位置:首页 > Python教程 > 正文内容

python爬虫代码示例分享_Python教程,python,爬虫,代码,示例

搜教程4年前 (2020-03-07)Python教程195

初学者编写python用什么软件_Python教程

初学者编写python可以使用的软件有Sublime Text、Vim、Visual Studio Code、PyCharm、Spyder、Thonny。其中前三个是代码编辑器,后三个是集成开发环境。

这篇文章主要介绍了三个python爬虫项目实例代码,使用了urllib2库,文中示例代码非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下。

python爬虫代码示例分享

一、爬取故事段子:推荐学习:Python视频教程

pycharm中文版界面如何设置?_Python教程

pycharm中文版界面设置:找到pycharm的文件安装位置,进入bin文件夹,然后将下载的汉化包粘贴到bin文件夹即可完成汉化。

注:部分代码无法正常运行,但仍有一定的参考价值。

#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+">(.*?)</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-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&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="//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)

三、爬取贴吧:

#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'<a\s+rel="noreferrer"\s+href="/p/\d+" rel="external nofollow" \s+title=".*?"\s+target="_blank" class="j_th_tit\s+">(.*?)</a>',re.S)
    titleList=partten.findall(html)
    rstr=r'<span\s+class="topic-tag"\s+data-name=".*?">#(.*?)#</span>'
    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教程栏目。

以上就是python爬虫代码示例分享的详细内容,更多请关注ki4网其它相关文章!

Python水仙花数的编程代码如何写_Python教程

Python水仙花数的编程代码写法:首先使用for in range(100,1000)进行循环;然后在循环体内通过计算得到各位数字;最后将各位数字使用pow()方法求立方再相加,并和原数进行判断即可。

扫描二维码推送至手机访问。

版权声明:本文由搜教程网发布,如需转载请注明出处。

本文链接:https://www.sojiaocheng.cn/17339.html

分享给朋友:

“python爬虫代码示例分享_Python教程,python,爬虫,代码,示例” 的相关文章

Python数据类型及运算符的细致总结(代码示例)【Python教程】,Python

Python数据类型及运算符的细致总结(代码示例)【Python教程】,Python

本篇文章给人人带来的内容是关于Python数据类型及运算符的细致总结(代码示例),有一定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。 前文报告了Python的输入输出以及变量的相干知识点,本节将讨论Python的数据类型以及数据之间的运算体式格局! 一、Python数据类型 上...

Python中迭代器和生成器以及列表的引见(附代码)【Python教程】,python

本篇文章给人人带来的内容是关于Python中迭代器和生成器以及列表的引见(附代码),有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。 迭代器在 Python 2.2 版本中被到场, 它为类序列对象供应了一个类序列的接口。 Python 的迭代无缝地支撑序列对象, 而且它还许可迭代...

自学python爬虫须要多久【Python教程】,python爬虫,自学python爬虫需要多久

自学python爬虫须要多久【Python教程】,python爬虫,自学python爬虫需要多久

如果是自学,从零基础末尾进修python的话,根据每个人明白能力的差别,大致上需求半年到一年半摆布的时刻,固然,如果有别的编程言语的阅历,入门照样比较快的,也许需求2~3个月能够用Python言语编写一些简朴的运用,只需举行系统的进修,才更好的控制Python妙技。【引荐教程:Python视频教程】...

(周全)python面试题的典范示例总结【Python教程】,python面试

本篇文章给人人带来的内容是关于(周全)python面试题的典范示例总结,有肯定的参考价值,有须要的朋侪可以参考一下,愿望对你有所协助。 1、Python怎样完成单例情势? Python有两种体式格局可以完成单例情势,下面两个例子运用了差别的体式格局完成单例情势: 1、 class S...

python怎样解释多行【Python教程】,python注释

python怎样解释多行【Python教程】,python注释

Python解释多行的要领有以下三种:运用ctrl+/完成多行解释、在每一行的开首运用shift+#键、输入''' '''或许""" """,将要解释的代码插在中心 我们常常在写代码的过程当中常常要解释代码。然则一行一行的解释比较贫苦,接下俩在文章中将为人人引见如安在Python言语中举行多行...

python怎样处置惩罚excel数据【Python教程】,python,处理excel数据

python怎样处置惩罚excel数据【Python教程】,python,处理excel数据

python主假如运用第三方模块库xlrd、xlwt、xluntils、pyExcelerator和Pandas来处置惩罚excel文件数据。 这里有一张excel数据表,下面我们经由过程示例来看看xlrd、xlwt、xluntils、pyExcelerator和Pandas是怎样处置惩罚...