吴裕雄 python 爬虫(2)
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)的更多相关文章
- 吴裕雄 python 爬虫(4)
import requests user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, li ...
- 吴裕雄 python 爬虫(3)
import hashlib md5 = hashlib.md5() md5.update(b'Test String') print(md5.hexdigest()) import hashlib ...
- 吴裕雄 python 爬虫(1)
from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm' o = urlparse(url) pr ...
- 吴裕雄--python学习笔记:爬虫基础
一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...
- 吴裕雄--python学习笔记:爬虫包的更换
python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...
- 吴裕雄--python学习笔记:爬虫
import chardet import urllib.request page = urllib.request.urlopen('http://photo.sina.com.cn/') #打开网 ...
- 吴裕雄 python 神经网络——TensorFlow pb文件保存方法
import tensorflow as tf from tensorflow.python.framework import graph_util v1 = tf.Variable(tf.const ...
- 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(4)
# -*- coding: utf-8 -*- import glob import os.path import numpy as np import tensorflow as tf from t ...
- 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(3)
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
随机推荐
- flask表单标签
环境:python2.7+flask+flask_wtf 内容:flask的部分常用表单标签 test.py # -*- coding: utf-8 -*- # flask web_form 表单控件 ...
- Linux交换空间(swap space)
每次安装Linux的时候,都会要求配置交换分区,那么这个分区是干嘛的呢?不设置这个分区有什么后果?如果一定要设置,设置多大比较合适?本篇将试图回答这些问题并尽量覆盖所有swap相关的知识. 下面的所有 ...
- elasticsearch License 到期后更新步骤
ELK下载安装后有一个月试用期,到期后需要更新License,且每个License有效期为 1 年,License过期前10天里相关log会一直出现,提醒用户更新,支持实时更新无需重启服务. 步骤: ...
- 1-scala基础
1.scala ①定义:Scala设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala运行于Java平台(Java虚拟机),并兼容现有的Java程序. ②特点:优雅.简洁.一行代码抵得上多 ...
- Java并发编程:Java Thread 的 sleep() 和 wait() 的区别
1. start 和 run 方法解释: 1) start: 用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码.通过调用Thread类 ...
- ORACLE 归档日志打开关闭方法
一 设置为归档方式 1 sql> archive log list; #查看是不是归档方式 2 sql> alter system set log_archive_start=true ...
- Java课程作业之动手动脑(五)
1.请阅读并运行AboutException.java示例. import javax.swing.*; class AboutException { public static void main( ...
- 代码: CSS3动画,简单示例(鼠标移上去后,背景图片旋转)
<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.2/jquery.mi ...
- 25.纯 CSS 创作一个慧星拖尾效果的 loader 动画
原文地址:https://segmentfault.com/a/1190000014916281 简化地址:https://codepen.io/pen/?editors=1100 HTML代码: & ...
- linux base shell 基础语法2
转载 http://blog.csdn.net/aggrelxf/article/details/8263110 单引 双引 反引用[] [[]] 将命令的输出读入一个变量中,可以将它放入双引号中,即 ...