import requests

from bs4 import BeautifulSoup

url = 'http://www.baidu.com'
html = requests.get(url)
sp = BeautifulSoup(html.text, 'html.parser')
print(sp)

html_doc = """
<html><head><title>页标题</title></head> <p class="title"><b>文件标题</b></p> <p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="story">...</p>
""" from bs4 import BeautifulSoup sp = BeautifulSoup(html_doc,'html.parser') print(sp.find('b')) # 返回值:<b>文件标题</b> print(sp.find_all('a')) #返回值: [<b>文件标题</b>] print(sp.find_all("a", {"class":"sister"})) data1=sp.find("a", {"href":"http://example.com/elsie"})
print(data1.text) # 返回值:Elsie data2=sp.find("a", {"id":"link2"})
print(data2.text) # 返回值:Lacie data3 = sp.select("#link3")
print(data3[0].text) # 返回值:Tillie print(sp.find_all(['title','a'])) data1=sp.find("a", {"id":"link1"})
print(data1.get("href")) #返回值: http://example.com/elsie

import requests

from bs4 import BeautifulSoup

url = 'http://www.wsbookshow.com/'
html = requests.get(url)
html.encoding="gbk" sp=BeautifulSoup(html.text,"html.parser")
links=sp.find_all(["a","img"]) # 同时读取 <a> 和 <img>
for link in links:
href=link.get("href") # 读取 href 属性的值
# 判断值是否为非 None,以及是不是以http://开头
if((href != None) and (href.startswith("http://"))):
print(href)

import requests

from bs4 import BeautifulSoup

url = 'http://www.taiwanlottery.com.tw/'
html = requests.get(url)
sp = BeautifulSoup(html.text, 'html.parser') data1 = sp.select("#rightdown")
print(data1)

data2 = data1[0].find('div', {'class':'contents_box02'})
print(data2)
print() data3 = data2.find_all('div', {'class':'ball_tx'})
print(data3)

import requests
from bs4 import BeautifulSoup url1 = 'http://www.pm25x.com/' #获得主页面链接
html = requests.get(url1) #抓取主页面数据
sp1 = BeautifulSoup(html.text, 'html.parser') #把抓取的数据进行解析 city = sp1.find("a",{"title":"北京PM2.5"}) #从解析结果中找出title属性值为"北京PM2.5"的标签
print(city)
citylink=city.get("href") #从找到的标签中取href属性值
print(citylink)
url2=url1+citylink #生成二级页面完整的链接地址
print(url2) html2=requests.get(url2) #抓取二级页面数据
sp2=BeautifulSoup(html2.text,"html.parser") #二级页面数据解析
#print(sp2)
data1=sp2.select(".aqivalue") #通过类名aqivalue抓取包含北京市pm2.5数值的标签
pm25=data1[0].text #获取标签中的pm2.5数据
print("北京市此时的PM2.5值为:"+pm25) #显示pm2.5值

import requests,os
from bs4 import BeautifulSoup
from urllib.request import urlopen url = 'http://www.tooopen.com/img/87.aspx' html = requests.get(url)
html.encoding="utf-8" sp = BeautifulSoup(html.text, 'html.parser') # 建立images目录保存图片
images_dir="E:\\images\\"
if not os.path.exists(images_dir):
os.mkdir(images_dir) # 取得所有 <a> 和 <img> 标签
all_links=sp.find_all(['a','img'])
for link in all_links:
# 读取 src 和 href 属性内容
src=link.get('src')
href = link.get('href')
attrs=[src,src]
for attr in attrs:
# 读取 .jpg 和 .png 檔
if attr != None and ('.jpg' in attr or '.png' in attr):
# 设置图片文件完整路径
full_path = attr
filename = full_path.split('/')[-1] # 取得图片名
ext = filename.split('.')[-1] #取得扩展名
filename = filename.split('.')[-2] #取得主文件名
if 'jpg' in ext: filename = filename + '.jpg'
else: filename = filename + '.png'
print(attr)
# 保存图片
try:
image = urlopen(full_path)
f = open(os.path.join(images_dir,filename),'wb')
f.write(image.read())
f.close()
except:
print("{} 无法读取!".format(filename))

吴裕雄 python 爬虫(2)的更多相关文章

  1. 吴裕雄 python 爬虫(4)

    import requests user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, li ...

  2. 吴裕雄 python 爬虫(3)

    import hashlib md5 = hashlib.md5() md5.update(b'Test String') print(md5.hexdigest()) import hashlib ...

  3. 吴裕雄 python 爬虫(1)

    from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm' o = urlparse(url) pr ...

  4. 吴裕雄--python学习笔记:爬虫基础

    一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...

  5. 吴裕雄--python学习笔记:爬虫包的更换

    python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...

  6. 吴裕雄--python学习笔记:爬虫

    import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...

  7. 吴裕雄 python 神经网络——TensorFlow pb文件保存方法

    import tensorflow as tf from tensorflow.python.framework import graph_util v1 = tf.Variable(tf.const ...

  8. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(4)

    # -*- coding: utf-8 -*- import glob import os.path import numpy as np import tensorflow as tf from t ...

  9. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(3)

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

随机推荐

  1. nodejs使用案例-mysql操作

    1.package.json: { "scripts": { "start": "node app.js" }, "devDepe ...

  2. Html5——视频标签使用

    video标签: 上面的例子使用一个 Ogg 文件,适用于Firefox.Opera 以及 Chrome 浏览器.要确保适用于 Safari 浏览器,视频文件必须是 MPEG4 类型.video 元素 ...

  3. 《Linux 性能及调优指南》2.4 基准工具

    翻译:飞哥 (http://hi.baidu.com/imlidapeng) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance a ...

  4. Java7 新特性: try-with-resources

    Try-with-resources是java7中一个新的异常处理机制,它能够很容易地关闭在try-catch语句块中使用的资源. 利用Try-Catch-Finally管理资源(旧的代码风格)在ja ...

  5. vue2.0混入mixins

    假设一个项目,首页不需要登录就可以直接进入,但是在首页中有各种其他的模块,这些模块中,有些需要登录权限,而有些则不需要登录权限,所以在进入这些模块的时候,我们都要判断当前的登录状态,那么我们应该怎么组 ...

  6. ES6学习笔记<二>arrow functions 箭头函数、template string、destructuring

    接着上一篇的说. arrow functions 箭头函数 => 更便捷的函数声明 document.getElementById("click_1").onclick = ...

  7. GNU coreutils

    内核实用程序,针对文本及文件操作.涉及到102条linux命令.命令列表:cp.install.ln.mv.ls.echo…… 常见选项 1.退出状态 2.备份选项 cp, install, ln, ...

  8. 《算法》第六章部分程序 part 6

    ▶ 书中第六章部分程序,包括在加上自己补充的代码,包括二分图最大匹配(最小顶点覆盖)的交替路径算法和 HopcroftKarp 算法 ● 二分图最大匹配(最小顶点覆盖)的交替路径算法 package ...

  9. winform/timer控件/权限设置/三级联动

    一.timer控件 组件--timer timer是一个线程,默认可以跨线程访问对象 属性:Enabled--可用性 Interval--间隔时间 Tick:间隔时间发生事件 二.三级联动 例: pu ...

  10. 2. 解决svn working copy locked问题

    解决办法: 产生这种情况大多是因为上次svn更新命令执行失败且被自动锁定了. 如果cleanup没有效果的话只好手动删除锁定文件. 就可以通过“运行”--“cmd”--cd 到svn项目的根目录下,然 ...