吴裕雄 实战python编程(3)
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编程(3)的更多相关文章
- 吴裕雄 实战PYTHON编程(10)
import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ...
- 吴裕雄 实战PYTHON编程(9)
import cv2 cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv ...
- 吴裕雄 实战PYTHON编程(8)
import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...
- 吴裕雄 实战PYTHON编程(7)
import os from win32com import client word = client.gencache.EnsureDispatch('Word.Application')word. ...
- 吴裕雄 实战PYTHON编程(6)
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode ...
- 吴裕雄 实战PYTHON编程(5)
text = '中华'print(type(text))#<class 'str'>text1 = text.encode('gbk')print(type(text1))#<cla ...
- 吴裕雄 实战PYTHON编程(4)
import hashlib md5 = hashlib.md5()md5.update(b'Test String')print(md5.hexdigest()) import hashlib md ...
- 吴裕雄 实战python编程(2)
from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...
- 吴裕雄 实战python编程(1)
import sqlite3 conn = sqlite3.connect('E:\\test.sqlite') # 建立数据库联接cursor = conn.cursor() # 建立 cursor ...
随机推荐
- 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示
一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...
- SiteMap Editor for Microsoft Dynamics CRM 2011 使用说明
How to connect to CRM environments using this tool If you already connected to a CRM deployment usin ...
- Apache Shiro在web开发安全框架中的应用
前阶段就hadoop的分享了一些内容,希望对新手入门的朋友有点帮助吧!对于hadoop新手入门的,还是比较推荐大快搜索的DKHadoop发行版,三节点标准版还是值得拥有的(三节点的标准版是可以免费下载 ...
- JQuery获得内容 - text()、html() 以及 val()
获得text()和html() <!DOCTYPE html><html><head><script src="/jquery/jquery-1.1 ...
- 使用scrapy框架爬取自己的博文(2)
之前写了一篇用scrapy框架爬取自己博文的博客,后来发现对于中文的处理一直有问题- - 显示的时候 [u'python\u4e0b\u722c\u67d0\u4e2a\u7f51\u9875\u76 ...
- [转]Oracle中trace的几种
Oracle中trace的几种 标签: 杂谈 我们在Oracle中在做troubleshooting的时候,经常要去做跟踪来查错,那今天就介绍几种trace的方法. 在这之前,我先说说10046事 ...
- Mybatis常见面试题 三
1.什么是mybatis? (1)mybatis是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动.创建连接.创建statem ...
- Android Drawable Mipmap Vector使用及Vector兼容
原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...
- socket编程时SIGPIPE信号的处理
如果在write调用期间对方关闭连接,视时间顺序的不同有以下几种情况: 1. 刚好在write调用之前对方关闭: write返回失败,同时产生SIGPIPE. 2. write调用过程中对方关闭: 返 ...
- Java技术专题之JVM你的内存泄露了吗?
一.从一个例子开始 关于JVM的内存泄露,让我们从下面一个例子开始吧,大家来判断一下如果按照下面这种分配方式,会不会出现内存泄露呢? import java.util.List; import jav ...