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. php删除字符串最后一位

    一.前言 从数据库中select()读取一对多的信息时,经常需要将取出的数组用某个特定的字符分割,然后拼接成字符串. 常见的语法格式: foreach ($arr as $key => $val ...

  2. Django跨域问题

    相关博客地址 同源策略与Jsonp 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可 ...

  3. SP1812 LCS2 - Longest Common Substring II

    能匹配上子串的节点对它的所有parent都有贡献 在树上转移即可 #include<cstdio> #include<algorithm> #include<cstrin ...

  4. rpm和yum的区别

    rpm 只能安装已经下载到本地机器上的rpm 包, yum能在线下载并安装rpm包,能更新系统,且还能自动处理包与包之间的依赖问题,这个是rpm 工具所不具备的.

  5. mysql5.7-windows安装配置

    sonar要求mysql5.6版本以上,所以安装一下最新的mysql5.7 采用相对名路径和命令行启动,这样是为了方便迁移.也提供了加入服务的指令,但没有进行测试 解压mysql的zip压缩包 解压后 ...

  6. 阿里云实现putty私钥登录全过程

    阿里云实现putty私钥登录全过程 1 putty生成公钥和私钥 1)putty生成公钥和私钥 记得在空白区域 滑动 2 公钥上传到阿里云 1)公钥上传  私钥存到本地 3 公钥绑定要登录的实例 4 ...

  7. Hibernate架构

    参照: http://blog.csdn.net/lovesummerforever/article/details/19169779 http://www.w3cschool.cn/hibernat ...

  8. postgresql----COPY之表与文件之间的拷贝

    postgresql提供了COPY命令用于表与文件(和标准输出,标准输入)之间的相互拷贝,copy to由表至文件,copy from由文件至表. 示例1.将整张表拷贝至标准输出 test=# cop ...

  9. 安装 docker管理 工具 页面 portainer

    sudo docker run -d -p 7998:9000 -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data ...

  10. uri,url和urn的区别以及URLEncoder

    java.net.URL类不提供对标准RFC2396规定的特殊字符的转义,因此需要调用者自己对URL各组成部分进行encode.而java.net.URI则会提供转义功能.因此The recommen ...