这里是前章,我们做一下预备。之前太多事情没能写博客~。。             (此博客只适合python3x,python2x请自行更改代码)

首先你要有bs4模块

windows下安装:pip3 install bs4,如果你电脑有python2x和python3x的话,在python3x中安装bs4请已管理员的身份运行cmd执行pip3 install bs4安装bs4。

linux下安装:sudo pip3 install bs4

还有urllib.request模块

windows下安装:pip3 install urllib.request,如果你电脑有python2x和python3x的话,在python3x中安装bs4请已管理员的身份运行cmd执行pip3 install urllib.request安装urllib.request模块

例子1:获取源码

from urllib.request import urlopen

from bs4 import BeautifulSoup

html=urlopen("http://wikipedia.org")

dgc=BeautifulSoup(html)

print(dgc)

输出图如下:

这里我忘记加自定义错误了,当然你也可以不加。保险起见还是加

例子二:匹配对应的标签

  1. from urllib.request import urlopen
  2.  
  3. from bs4 import BeautifulSoup
    try:
    html=urlopen("http://dlszx.dgjy.net/")
    except EOFError as a:
    print("404 ")
    except:
    print("404")
    dgc=BeautifulSoup(html)
  1. fbc=dgc.findAll("img",{"src":"uploadfile/201762105219962.jpg"})
    print(fbc)

例子3:正则匹配所有对应的标签

不会正则的请去学习

  1. from urllib.request import urlopen
    import re
    from bs4 import BeautifulSoup
    try:
    html=urlopen("http://dlszx.dgjy.net/")
    except EOFError as a:
    print("404 ")
    except:
    print("404")
    dgc=BeautifulSoup(html)
    fbc=dgc.findAll("img",{"src":re.compile("img/.*?\.jpg")})
    for inks in fbc:
    print(inks)
    注意事项!!!:不要拿findAll去搜索引擎匹配,乱的你想死
    搜索引擎正则匹配要求很高:http:\/\/[a-zA-z].*?\[a-z]

例子4:

匹配网站所有的链接

  1.  
  1. from urllib.request import urlopen
    import re
    from bs4 import BeautifulSoup
    try:
    html=urlopen("http://wikipeda.org")
    except EOFError as a:
    print("EOFError")
  1. except:
    print("I dont EOFError")
  1. gfc=BeautifulSoup(html)
    for inks in gfc.findAll("a")
    if 'href' in inks.attrs:
    print("inks.attrs["href"]")
  1. 现在的时间是
  1. 2017-8-13-13:38

python网络数据采集(伴奏曲)的更多相关文章

  1. 笔记之Python网络数据采集

    笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...

  2. Python网络数据采集7-单元测试与Selenium自动化测试

    Python网络数据采集7-单元测试与Selenium自动化测试 单元测试 Python中使用内置库unittest可完成单元测试.只要继承unittest.TestCase类,就可以实现下面的功能. ...

  3. Python网络数据采集6-隐含输入字段

    Python网络数据采集6-隐含输入字段 selenium的get_cookies可以轻松获取所有cookie. from pprint import pprint from selenium imp ...

  4. Python网络数据采集4-POST提交与Cookie的处理

    Python网络数据采集4-POST提交与Cookie的处理 POST提交 之前访问页面都是用的get提交方式,有些网页需要登录才能访问,此时需要提交参数.虽然在一些网页,get方式也能提交参.比如h ...

  5. Python网络数据采集3-数据存到CSV以及MySql

    Python网络数据采集3-数据存到CSV以及MySql 先热热身,下载某个页面的所有图片. import requests from bs4 import BeautifulSoup headers ...

  6. Python网络数据采集2-wikipedia

    Python网络数据采集2-wikipedia 随机链接跳转 获取维基百科的词条超链接,并随机跳转.可能侧边栏和低栏会有其他链接.这不是我们想要的,所以定位到正文.正文在id为bodyContent的 ...

  7. Python网络数据采集1-Beautifulsoup的使用

    Python网络数据采集1-Beautifulsoup的使用 来自此书: [美]Ryan Mitchell <Python网络数据采集>,例子是照搬的,觉得跟着敲一遍还是有作用的,所以记录 ...

  8. Python网络数据采集PDF

    Python网络数据采集(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/16c4GjoAL_uKzdGPjG47S4Q 提取码:febb 复制这段内容后打开百度网盘手 ...

  9. python网络数据采集的代码

    python网络数据采集的代码 https://github.com/REMitchell/python-scraping

  10. [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等

    Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...

随机推荐

  1. Cenos 6.5上的subverion的yum配置笔记

    Subversion在CenOS 6.5上的安装配置 1.安装 yum install subversion 2.配置   #创建目录   mkdir /opt/svn   #创建版本库   svna ...

  2. 伪列:Oracle显示查询结果前几条记录用rownum<=。去掉重复记录,保留最早录入记录:取出最小ROWID

    显示6-10行记录: 去掉重复记录,保留最早录入记录:取出最小ROWID SELECT deptno,dname,loc,min(ROWID) FROM dept GROUP BY deptno,dn ...

  3. 【java设计模式】【创建模式Creational Pattern】建造模式Builder Pattern

    package com.tn.pattern; public class Client { public static void main(String[] args) { Director dire ...

  4. 字符串输入时的strlen()与\0

    在做串的数据结构时,被字符串printf %s输出时的结尾判断.strlen();长度.自定义StrCopy();字符串复制函数.StrAssign();字符串赋值函数卡了一下,固写此博巩固相关知识点 ...

  5. java随机生成验证码

    package com.yuyuchen.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; im ...

  6. MySQL datetime的更新,删除网上的一些老概念

    网上的老概念 第一点:是以前的MySQL的datetime的最小值是:'1000-01-01 00:00:00'(貌似),但是最新的MySQL测试datetime的最小值可以是:'0000-00-00 ...

  7. C#生成缩略图 (通用模式)

    用数据库保存图片的路径和文件名称,把文件保存在文件夹中.//保存在数据库中的文件路径ArrayList arrFilePath=new ArrayList();arrFilePath=myCommon ...

  8. input 上传图片显示预览、调用摄像头,ios和Android的兼容性解决

    html代码: <img id="pic" src="img/pic.png"/> </span><input id=" ...

  9. @NotEmpty、@NotBlank、@NotNull的区别

    @NotEmpty 用在集合类上面  @NotBlank 用在String上面  @NotNull 用在基本类型上 只有简单的结果,但是再更具体一点的内容就搜不到了,所以去看了看源码,发现了如下的注释 ...

  10. JavaScript的DOM编程--07--节点的属性

    节点的属性: 1). nodeName: 代表当前节点的名字. 只读属性. 如果给定节点是一个文本节点, nodeName 属性将返回内容为 #text 的字符串 2). nodeType:返回一个整 ...