python学习第四天

一、Xpath

  1. 定义:XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的计算机语言。XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力。
  2. Xpath解析匹配规则
    / 代表选取直接子节点
    // 代表选择任意子孙节点
    . 代表选取当前节点
    .. 代表选取当前节点的父节点
    @ 表示属性的限定,选取匹配属性的特定节点
  3. 解析例子
    index.html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>欢迎来到王者荣耀</h1>
<ul>
    <li><a href="herodetail/506.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg" width="91" height="91" alt="云中君">云中君</a></li>
    <li><a href="herodetail/505.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/505/505.jpg" width="91" height="91" alt="瑶">瑶</a></li>
    <li><a href="herodetail/529.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/529/529.jpg" width="91" height="91" alt="盘古">盘古</a></li>
    <li><a href="herodetail/511.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/511/511.jpg" width="91" height="91" alt="猪八戒">猪八戒</a></li>
    <li><a href="herodetail/515.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg" width="91" height="91" alt="嫦娥">嫦娥</a></li>
    <li><a href="herodetail/513.shtml" target="_blank"><img src="//game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg" width="91" height="91" alt="上官婉儿">上官婉儿</a></li>
</ul>
<ol>
    <li>坦克</li>
    <li>战士</li>
    <li>刺客</li>
</ol>
<div>这是第1个div标签</div>
<div>这是第2个div标签</div>
<div>这是第3个div标签</div>
<div id="container">
    <p>欢迎拉到百度</p>
    <a href="www.baidu.com">百度</a>
</div>
</body>
</html>

python文件:

with open("./index.html","r",encoding="UTF-8")as f:
    html_data = f.read()
    selector = html.fromstring(html_data)
    h1=selector.xpath("/html/body/h1/text()")#要获取标签内容使用“/text()”,“/”表示根目录
    print(h1[0])

    p = selector.xpath("//div[@id='container']/p/text()")#“//”匹配任意目录
    phref = selector.xpath("//div[@id='container']/a/@href")#“//”匹配任意目录
    print(p[0])
    print(phref[0])

输出结果

欢迎来到王者荣耀
欢迎拉到百度
www.baidu.com

二、使用requests获取网站响应

url = "http://www.baidu.com"
response = requests.get(url)
print(response)
print(response.status_code)#获取状态码
print(response.headers)#获取响应头
print(response.content)#获取bytes类型的响应
print(response.text)#获取str类型的响应

有些网站需要带上请求头headers才能得到网站响应,如知乎,否则会返回状态400 Bad Request

# 使用字典定义请求头能得到正确的响应
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
resp = requests.get('https://www.zhihu.com/', headers = headers)
print(resp.status_code)

三、爬取网站信息

爬取当当网某图书的信息

import requests
from lxml import html
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def spider_dangdang(isbn):
    book_list = []
    url = 'http://search.dangdang.com/?key={}&act=input'.format(isbn)
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
    resp = requests.get(url, headers=headers)
    html_data = resp.text
    # with open('dangdang.html', 'w', encoding='utf-8') as f:
    #     f.write(html_data)

    # 提取目标站的信息
    selector = html.fromstring(html_data)
    ul_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li')
    print('共有{}家店铺售卖此图书'.format(len(ul_list)))

    #遍历ul_list
    for li in ul_list:
        title = li.xpath('./a/@title')[0].strip()#  图书名称
        #print(title)
        price = li.xpath('./p[@class="price"]/span[@class="search_now_price"]/text()')[0]#  图书价格
        price = float(price.replace("¥",""))
        # print(price)
        href = li.xpath('./a/@href')[0]  #  图书购买链接
        # print(href)
        shop = li.xpath('./p[@class="search_shangjia"]/a/text()')# 图书卖家名称
        shop = '当当自营' if len(shop) == 0 else shop[0]
        # print(shop)
        book_list.append({
            'title':title,
            'price':price,
            'href':href,
            'shop':shop
        })

    book_list.sort(key=lambda x:x['price'])

    for book in book_list:
        print(book)

    top10_shop = [book_list[i] for i in range(1,11)]
    print(top10_shop)
    shop = [x['shop'] for x in top10_shop]
    print(shop)
    price = [y['price'] for y in top10_shop]
    print(price)
    plt.barh(shop,price)
    plt.show()

    df = pd.DataFrame(book_list)
    df.to_csv('dangdang.csv')

spider_dangdang('9787115428028')
某图书当当价格最低的十家

爬取豆瓣电影上映信息

import requests
from lxml import html
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

url = 'https://movie.douban.com/cinema/later/chongqing/'
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"}
resp = requests.get(url, headers=headers)
html_data = resp.text
selector = html.fromstring(html_data)
movie_list = []
div_list = selector.xpath('//div[@id="showing-soon"]/div')
for div in div_list:
    name = div.xpath('./div/h3/a/text()')[0]
    date = div.xpath('./div/ul/li[1]/text()')[0]
    type = div.xpath('./div/ul/li[2]/text()')[0]
    country = div.xpath('./div/ul/li[3]/text()')[0]
    number = div.xpath('./div/ul/li[4]/span/text()')[0]
    number = int(number.replace("人想看",""))
    movie_list.append({
        "name":name,
        "date":date,
        "type":type,
        "country":country,
        "number":number
    })

movie_list.sort(key=lambda x:x['number'],reverse=True)
print(movie_list)

counts = {}
for movie in movie_list:
    counts[movie['country']] = counts.get(movie['country'], 0) + 1


countrys = []
countrys_count = []
for k,v in counts.items():
    countrys.append(k)
    countrys_count.append(v)
print(countrys)
print(countrys_count)
plt.pie(countrys_count,labels=countrys,autopct = '%1.1f%%')
plt.show()

#绘制top5最想看的电影
top5_movie = [movie_list[i] for i in range(5)]
x = [x['name'] for x in top5_movie]
y = [y['number'] for y in top5_movie]
plt.barh(x, y)
plt.show()
电影国家占比
想看人数前五
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,104评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,816评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,697评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,836评论 1 298
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,851评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,441评论 1 310
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,992评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,899评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,457评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,529评论 3 341
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,664评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,346评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,025评论 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,511评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,611评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,081评论 3 377
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,675评论 2 359

推荐阅读更多精彩内容

  • 爬虫 使用lxml下的html包解析的fromstringHTML文件 xpath()方法:能将字符串转化为标签,...
    缄墨_1427阅读 187评论 0 0
  • 爬虫----大数据 XPath语法和lxml模块 一、 提取本地html中的数据 新建html文件 读取 使用x...
    余生只有一个容EuniceTo阅读 410评论 0 0
  • 今天是学习python的第四天,接着昨天的内容学习。 使用json技术去爬虫 首先先解释下,什么是json ,j...
    小丑表演阅读 173评论 2 1
  • 爬虫 1.提取本地html中的数据 用Lxml (1).新建html文件 (2).读取 (3).使用Lxml中的x...
    蓝彭友_1121阅读 193评论 0 0
  • 练习爬虫爬取网页电影信息 -首先,导入包 -设置域名 -设置爬取信息时的访问用户信息 之前列表字典的区别有些没分清...
    幻的風阅读 180评论 0 0