python爬取w3shcool的JQuery课程并且保存到本地


Posted in Python onApril 06, 2017

最近在忙于找工作,闲暇之余,也找点爬虫项目练练手,写写代码,知道自己是个菜鸟,但是要多加练习,书山有路勤为径。各位爷有测试坑可以给我介绍个啊,自动化,功能,接口都可以做。

首先呢,我们明确需求,很多同学呢,有事没事就想看看一些技术,比如我想看看JQuery的语法呢,可是我现在没有网络,手机上也没有电子书,真的让我们很难受,那么别着急啊,你这需求我在这里满足你,首先呢,你的需求是获取JQuery的语法的,那么我在看到这个需求,我有响应的网站那么我们接下来去分析这个网站。http://www.w3school.com.cn/jquery/jquery_syntax.asp 这是语法url, http://www.w3school.com.cn/jquery/jquery_intro.asp 这是简介的url,那么我们拿到很多的url分析到,我们的http://www.w3school.com.cn/jquery是相同的,那么我们在来分析在界面怎么可以获取得到这些,我们可以看到右面有相应的目标栏,那么我们去分析下

python爬取w3shcool的JQuery课程并且保存到本地

我们来看下这些链接,。我们可以吧这些链接和http://www.w3school.com.cn拼接到一起。然后组成我们新的url,

上代码

import urllib.request
from bs4 import BeautifulSoup 
import time
def head():
 headers={
 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
 }
 return headers
def parse_url(url):
 hea=head()
 resposne=urllib.request.Request(url,headers=hea)
 html=urllib.request.urlopen(resposne).read().decode('gb2312')
 return html
def url_s():
 url='http://www.w3school.com.cn/jquery/index.asp'
 html=parse_url(url)
 soup=BeautifulSoup(html)
 me=soup.find_all(id='course')
 m_url_text=[]
 m_url=[]
 for link in me:
  m_url_text.append(link.text)
  m=link.find_all('a')
  for i in m:
   m_url.append(i.get('href'))
 for i in m_url_text:
  h=i.encode('utf-8').decode('utf-8')
  m_url_text=h.split('\n')
 return m_url,m_url_text

这样我们使用url_s这个函数就可以获取我们所有的链接。

['/jquery/index.asp', '/jquery/jquery_intro.asp', '/jquery/jquery_install.asp', '/jquery/jquery_syntax.asp', '/jquery/jquery_selectors.asp', '/jquery/jquery_events.asp', '/jquery/jquery_hide_show.asp', '/jquery/jquery_fade.asp', '/jquery/jquery_slide.asp', '/jquery/jquery_animate.asp', '/jquery/jquery_stop.asp', '/jquery/jquery_callback.asp', '/jquery/jquery_chaining.asp', '/jquery/jquery_dom_get.asp', '/jquery/jquery_dom_set.asp', '/jquery/jquery_dom_add.asp', '/jquery/jquery_dom_remove.asp', '/jquery/jquery_css_classes.asp', '/jquery/jquery_css.asp', '/jquery/jquery_dimensions.asp', '/jquery/jquery_traversing.asp', '/jquery/jquery_traversing_ancestors.asp', '/jquery/jquery_traversing_descendants.asp', '/jquery/jquery_traversing_siblings.asp', '/jquery/jquery_traversing_filtering.asp', '/jquery/jquery_ajax_intro.asp', '/jquery/jquery_ajax_load.asp', '/jquery/jquery_ajax_get_post.asp', '/jquery/jquery_noconflict.asp', '/jquery/jquery_examples.asp', '/jquery/jquery_quiz.asp', '/jquery/jquery_reference.asp', '/jquery/jquery_ref_selectors.asp', '/jquery/jquery_ref_events.asp', '/jquery/jquery_ref_effects.asp', '/jquery/jquery_ref_manipulation.asp', '/jquery/jquery_ref_attributes.asp', '/jquery/jquery_ref_css.asp', '/jquery/jquery_ref_ajax.asp', '/jquery/jquery_ref_traversing.asp', '/jquery/jquery_ref_data.asp', '/jquery/jquery_ref_dom_element_methods.asp', '/jquery/jquery_ref_core.asp', '/jquery/jquery_ref_prop.asp'], ['jQuery 教程', '', 'jQuery 教程', 'jQuery 简介', 'jQuery 安装', 'jQuery 语法', 'jQuery 选择器', 'jQuery 事件', '', 'jQuery 效果', '', 'jQuery 隐藏/显示', 'jQuery 淡入淡出', 'jQuery 滑动', 'jQuery 动画', 'jQuery stop()', 'jQuery Callback', 'jQuery Chaining', '', 'jQuery HTML', '', 'jQuery 获取', 'jQuery 设置', 'jQuery 添加', 'jQuery 删除', 'jQuery CSS 类', 'jQuery css()', 'jQuery 尺寸', '', 'jQuery 遍历', '', 'jQuery 遍历', 'jQuery 祖先', 'jQuery 后代', 'jQuery 同胞', 'jQuery 过滤', '', 'jQuery AJAX', '', 'jQuery AJAX 简介', 'jQuery 加载', 'jQuery Get/Post', '', 'jQuery 杂项', '', 'jQuery noConflict()', '', 'jQuery 实例', '', 'jQuery 实例', 'jQuery 测验', '', 'jQuery 参考手册', '', 'jQuery 参考手册', 'jQuery 选择器', 'jQuery 事件', 'jQuery 效果', 'jQuery 文档操作', 'jQuery 属性操作', 'jQuery CSS 操作', 'jQuery Ajax', 'jQuery 遍历', 'jQuery 数据', 'jQuery DOM 元素', 'jQuery 核心', 'jQuery 属性', '', ''])

这是所有链接还有对应链接的所对应的语法模块的名字。那么我们接下来就是去拼接urls,使用的是str的拼接

['http://www.w3school.com.cn//jquery/index.asp', 'http://www.w3school.com.cn//jquery/jquery_intro.asp', 'http://www.w3school.com.cn//jquery/jquery_install.asp', 'http://www.w3school.com.cn//jquery/jquery_syntax.asp', 'http://www.w3school.com.cn//jquery/jquery_selectors.asp', 'http://www.w3school.com.cn//jquery/jquery_events.asp', 'http://www.w3school.com.cn//jquery/jquery_hide_show.asp', 'http://www.w3school.com.cn//jquery/jquery_fade.asp', 'http://www.w3school.com.cn//jquery/jquery_slide.asp', 'http://www.w3school.com.cn//jquery/jquery_animate.asp', 'http://www.w3school.com.cn//jquery/jquery_stop.asp', 'http://www.w3school.com.cn//jquery/jquery_callback.asp', 'http://www.w3school.com.cn//jquery/jquery_chaining.asp', 'http://www.w3school.com.cn//jquery/jquery_dom_get.asp', 'http://www.w3school.com.cn//jquery/jquery_dom_set.asp', 'http://www.w3school.com.cn//jquery/jquery_dom_add.asp', 'http://www.w3school.com.cn//jquery/jquery_dom_remove.asp', 'http://www.w3school.com.cn//jquery/jquery_css_classes.asp', 'http://www.w3school.com.cn//jquery/jquery_css.asp', 'http://www.w3school.com.cn//jquery/jquery_dimensions.asp', 'http://www.w3school.com.cn//jquery/jquery_traversing.asp', 'http://www.w3school.com.cn//jquery/jquery_traversing_ancestors.asp', 'http://www.w3school.com.cn//jquery/jquery_traversing_descendants.asp', 'http://www.w3school.com.cn//jquery/jquery_traversing_siblings.asp', 'http://www.w3school.com.cn//jquery/jquery_traversing_filtering.asp', 'http://www.w3school.com.cn//jquery/jquery_ajax_intro.asp', 'http://www.w3school.com.cn//jquery/jquery_ajax_load.asp', 'http://www.w3school.com.cn//jquery/jquery_ajax_get_post.asp', 'http://www.w3school.com.cn//jquery/jquery_noconflict.asp', 'http://www.w3school.com.cn//jquery/jquery_examples.asp', 'http://www.w3school.com.cn//jquery/jquery_quiz.asp', 'http://www.w3school.com.cn//jquery/jquery_reference.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_selectors.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_events.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_effects.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_manipulation.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_attributes.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_css.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_ajax.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_traversing.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_data.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_dom_element_methods.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_core.asp', 'http://www.w3school.com.cn//jquery/jquery_ref_prop.asp']

那么我们有这个所有的urls,那么我们来分析下,文章正文。

分析可以得到我们的所有的正文都是在一个id=maincontent中,那么我们直接解析每个界面中的id=maincontent的标签,获取响应的text文档,并且保存就好。

所以我们所有的代码如下:

import urllib.request
from bs4 import BeautifulSoup 
import time
def head():
 headers={
 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
 }
 return headers
def parse_url(url):
 hea=head()
 resposne=urllib.request.Request(url,headers=hea)
 html=urllib.request.urlopen(resposne).read().decode('gb2312')
 return html
def url_s():
 url='http://www.w3school.com.cn/jquery/index.asp'
 html=parse_url(url)
 soup=BeautifulSoup(html)
 me=soup.find_all(id='course')
 m_url_text=[]
 m_url=[]
 for link in me:
  m_url_text.append(link.text)
  m=link.find_all('a')
  for i in m:
   m_url.append(i.get('href'))
 for i in m_url_text:
  h=i.encode('utf-8').decode('utf-8')
  m_url_text=h.split('\n')
 return m_url,m_url_text
def xml():
 url,url_text=url_s()
 url_jque=[]
 for link in url:
  url_jque.append('http://www.w3school.com.cn/'+link)
 return url_jque
def xiazai():
 urls=xml()
 i=0
 for url in urls:
  html=parse_url(url)
  soup=BeautifulSoup(html)
  me=soup.find_all(id='maincontent')
  with open(r'%s.txt'%i,'wb') as f:
   for h in me:
    f.write(h.text.encode('utf-8'))
    print(i)
  i+=1
if __name__ == '__main__':
 xiazai()
import urllib.request
from bs4 import BeautifulSoup 
import time
def head():
 headers={
 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'
 }
 return headers
def parse_url(url):
 hea=head()
 resposne=urllib.request.Request(url,headers=hea)
 html=urllib.request.urlopen(resposne).read().decode('gb2312')
 return html
def url_s():
 url='http://www.w3school.com.cn/jquery/index.asp'
 html=parse_url(url)
 soup=BeautifulSoup(html)
 me=soup.find_all(id='course')
 m_url_text=[]
 m_url=[]
 for link in me:
  m_url_text.append(link.text)
  m=link.find_all('a')
  for i in m:
   m_url.append(i.get('href'))
 for i in m_url_text:
  h=i.encode('utf-8').decode('utf-8')
  m_url_text=h.split('\n')
 return m_url,m_url_text

def xml():
 url,url_text=url_s()
 url_jque=[]
 for link in url:
  url_jque.append('http://www.w3school.com.cn/'+link)
 return url_jque
def xiazai():
 urls=xml()
 i=0
 for url in urls:
  html=parse_url(url)
  soup=BeautifulSoup(html)
  me=soup.find_all(id='maincontent')
  with open(r'%s.txt'%i,'wb') as f:
   for h in me:
    f.write(h.text.encode('utf-8'))
    print(i)
  i+=1
if __name__ == '__main__':
 xiazai()

结果

python爬取w3shcool的JQuery课程并且保存到本地

好了至此,我们的爬取工作完成,剩下的就是小修小布,大的内容我们都应该完成了。

其实python的爬虫还是很简单的,只要我们会分析网站的元素,找出所有元素的通项就可以很好的去分析和解决我们的问题

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Python 相关文章推荐
Python入门篇之正则表达式
Oct 20 Python
python使用xlrd模块读写Excel文件的方法
May 06 Python
Python Sql数据库增删改查操作简单封装
Apr 18 Python
python 匹配url中是否存在IP地址的方法
Jun 04 Python
Python编程flask使用页面模版的方法
Dec 28 Python
python安装scipy的方法步骤
Jun 26 Python
Python实现元素等待代码实例
Nov 11 Python
Python内置类型性能分析过程实例
Jan 29 Python
python异常处理try except过程解析
Feb 03 Python
使用遗传算法求二元函数的最小值
Feb 11 Python
Python爬虫实现HTTP网络请求多种实现方式
Jun 19 Python
Python实现GIF图倒放
Jul 16 Python
使用Python对SQLite数据库操作
Apr 06 #Python
使用Python对MySQL数据操作
Apr 06 #Python
windows 10下安装搭建django1.10.3和Apache2.4的方法
Apr 05 #Python
Python使用迭代器捕获Generator返回值的方法
Apr 05 #Python
由浅入深讲解python中的yield与generator
Apr 05 #Python
Python中shutil模块的学习笔记教程
Apr 04 #Python
python 遍历字符串(含汉字)实例详解
Apr 04 #Python
You might like
德生BCL3000的电路分析和打磨
2021/03/02 无线电
通过JavaScript或PHP检测Android设备的代码
2011/03/09 PHP
php中用foreach来操作数组的代码
2011/07/17 PHP
destoon安全设置中需要设置可写权限的目录及文件
2014/06/21 PHP
一个经典的PHP文件上传类分享
2014/11/18 PHP
php中字符查找函数strpos、strrchr与strpbrk用法
2014/11/18 PHP
php mysql数据库操作类(实例讲解)
2017/08/06 PHP
php curl操作API接口类完整示例
2019/05/21 PHP
PHP加MySQL消息队列深入理解
2021/02/27 PHP
js字符编码函数区别分析
2011/12/28 Javascript
Node.js巧妙实现Web应用代码热更新
2015/10/22 Javascript
VueJs与ReactJS和AngularJS的异同点
2016/12/12 Javascript
jQuery Easyui datagrid连续发送两次请求问题
2016/12/13 Javascript
js实现多行文本框统计剩余字数功能
2017/03/28 Javascript
jquery与js实现全选功能的区别
2017/06/11 jQuery
Angular4的输入属性与输出属性实例详解
2017/11/29 Javascript
使用vue的transition完成滑动过渡的示例代码
2018/06/25 Javascript
Vue监听事件实现计数点击依次增加的方法
2018/09/26 Javascript
JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
2018/12/14 Javascript
vue+element 模态框表格形式的可编辑表单实现
2019/06/07 Javascript
区分vue-router的hash和history模式
2020/10/03 Javascript
如何使用gpu.js改善JavaScript的性能
2020/12/01 Javascript
python多线程方式执行多个bat代码
2016/06/07 Python
解决PyCharm同目录下导入模块会报错的问题
2018/10/13 Python
将Python字符串生成PDF的实例代码详解
2019/05/17 Python
已安装tensorflow-gpu,但keras无法使用GPU加速的解决
2020/02/07 Python
python在不同条件下的输入与输出
2020/02/13 Python
如何用Matplotlib 画三维图的示例代码
2020/07/28 Python
css3制作彩色边线3d立体按钮的示例(css3按钮)
2014/05/06 HTML / CSS
美国批发零售网站:GearXS
2016/07/26 全球购物
世界上最大的曲棍球商店:Pro Hockey Life
2017/10/30 全球购物
机电工程学生自荐信范文
2013/12/07 职场文书
《月亮湾》教学反思
2014/04/14 职场文书
学生鉴定评语大全
2014/05/05 职场文书
怎样写工作总结啊!
2019/06/18 职场文书
Python合并多张图片成PDF
2021/06/09 Python