學(xué)習(xí)Python的第四天

爬蟲學(xué)習(xí)

# -*- coding: utf-8 -*-
# @Time    : 2019/7/31 11:28
# @Author  : Eric Lee
# @Email   : li.yan_li@neusoft.com
# @File    : spider_dangdang.py
# @Software: PyCharm
import requests
 from lxml import html
def spider_dangdang(isbn):
# 目標(biāo)站點(diǎn)地址
url = 'http://search.dangdang.com/?key={}&act=input'.format(isbn)
# print(url)
# 獲取站點(diǎn)str類型的響應(yīng)
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
#  將html頁面寫入本地
# with open('dangdang.html', 'w', encoding='utf-8') as f:
#     f.write(html_data)

# 提取目標(biāo)站的信息
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)
    #  圖書購買鏈接
    link = li.xpath('a/@href')[0]
    print(link)
    #  圖書價(jià)格
    price = li.xpath('./p[@class="price"]/span[@class="search_now_price"]/text()')[0]
    price = float(price.replace('¥',''))
    print(price)
    # 圖書賣家名稱
    store = li.xpath('./p[@class="search_shangjia"]/a/text()')
    # if len(store) == 0:
    #     store = '當(dāng)當(dāng)自營'
    # else:
    #     store = store[0]
    store = '當(dāng)當(dāng)自營' if len(store) == 0 else store[0]
    print(store)

XPath

XPath 節(jié)點(diǎn)

節(jié)點(diǎn)
在 XPath 中线得,有七種類型的節(jié)點(diǎn):元素、屬性裸扶、文本框都、命名空間、處理指令呵晨、注釋以及文檔(根)節(jié)點(diǎn)。XML 文檔是被作為節(jié)點(diǎn)樹來對(duì)待的熬尺。樹的根被稱為文檔節(jié)點(diǎn)或者根節(jié)點(diǎn)摸屠。

請(qǐng)看下面這個(gè) XML 文檔:

    <?xml version="1.0" encoding="UTF-8"?>

  <bookstore>
  <book>
   <title lang="en">Harry Potter</title>
   <author>J K. Rowling</author>
   <year>2005</year>
   <price>29.99</price>
 </book>
</bookstore>

上面的XML文檔中的節(jié)點(diǎn)例子:
<bookstore> (文檔節(jié)點(diǎn))
<author>J K. Rowling</author> (元素節(jié)點(diǎn))
lang="en" (屬性節(jié)點(diǎn))

基本值(或稱原子值,Atomic value)
基本值是無父或無子的節(jié)點(diǎn)粱哼。
基本值的例子:
J K. Rowling
"en"
項(xiàng)目(Item)
項(xiàng)目是基本值或者節(jié)點(diǎn)季二。

節(jié)點(diǎn)關(guān)系
父(Parent)
每個(gè)元素以及屬性都有一個(gè)父。
在下面的例子中揭措,book 元素是 title胯舷、author、year 以及 price 元素的父:

 <book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

子(Children)
元素節(jié)點(diǎn)可有零個(gè)绊含、一個(gè)或多個(gè)子桑嘶。
在下面的例子中,title躬充、author逃顶、year 以及 price 元素都是 book 元素的子:

 <book>
     <title>Harry Potter</title>
   <author>J K. Rowling</author>
   <year>2005</year>
   <price>29.99</price>
</book>

同胞(Sibling)
擁有相同的父的節(jié)點(diǎn)
在下面的例子中,title充甚、author以政、year 以及 price 元素都是同胞:

<book>
  <title>Harry Potter</title>
 <author>J K. Rowling</author>
  <year>2005</year>
 <price>29.99</price>
</book>

先輩(Ancestor)
某節(jié)點(diǎn)的父、父的父伴找,等等盈蛮。
在下面的例子中,title 元素的先輩是 book 元素和 bookstore 元素:

 <bookstore>
   <book>
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
   <price>29.99</price>
 </book>

 </bookstore>

后代(Descendant)
某個(gè)節(jié)點(diǎn)的子技矮,子的子抖誉,等等。
在下面的例子中穆役,bookstore 的后代是 book寸五、title、author耿币、year 以及 price 元素:

  <bookstore>
     <book>
     <title>Harry Potter</title>
     <author>J K. Rowling</author>
     <year>2005</year>
      <price>29.99</price>
 </book>
 </bookstore>

選取節(jié)點(diǎn)
image

電影top5

  import requests
  from lxml import html
  import pandas as pd
 import jieba
 from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def Film():
# 目標(biāo)站點(diǎn)地址
url = 'https://movie.douban.com/cinema/later/chongqing/'
header = {"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=header)
html_data = resp.text
# 提取目標(biāo)站的信息
selector = html.fromstring(html_data)
film = selector.xpath('//div[@id="showing-soon"]/div')
print(film)
div_list = []
for film_list in film:
    # 電影名
    title_list = film_list.xpath('./div/h3/a/text()')[0]
    print(title_list)
    # 上映時(shí)間
    time_list = film_list.xpath('./div/ul/li[1]/text()')[0]
    print(time_list)
    # 電影類型
    type_list = film_list.xpath('./div/ul/li[2]/text()')[0]
    print(type_list)
    # 上映國家
    con_list = film_list.xpath('./div/ul/li[3]/text()')[0]
    print(con_list)
    # 想看人數(shù)
    number_list = film_list.xpath('./div/ul/li[4]/span/text()')[0]
    print(number_list)
    # 替換
    number_list = int(number_list.replace('人想看',''))
    # 添加電影信息
    div_list.append({
        'title': title_list,
        'time': time_list,
        'type': type_list,
        'con': con_list,
        'number': number_list
    })
    # 按照想看人數(shù)排序
div_list.sort(key=lambda x:x['number'], reverse=True )
print(div_list)
# 遍歷
for items_list in div_list:
    print(items_list)
# 繪制top5最想看的電影占比圖
# 提取前五部電影信息
top5_store = [div_list[i] for i in range(5)]
# 提取電影名
x = [x['title'] for x in top5_store]
print(x)
# 提取想看人數(shù)
y = [x['number'] for x in top5_store]
print(y)
explode = [0.1, 0, 0, 0, 0]
plt.pie(y, explode=explode, labels=x, shadow=True, autopct='%1.1f%%')
plt.axis('equal')
plt.legend(loc=2)
plt.show()

# 繪制即將上映電影國家的占比圖
counts = {}
# 提取所有上映國家
s = [x['con'] for x in div_list]
print(s)
# 統(tǒng)計(jì)上映國家與數(shù)量
for word in s:
    counts[word] = counts.get(word, 0) + 1
print(counts)
# 提取上映國家
name = counts.keys()
print(name)
# 提取數(shù)量
number = counts.values()
print(number)
explode1 = [0.1, 0, 0, 0]
plt.pie(number, explode=explode1, labels=name, shadow=True, autopct='%1.1f%%')
plt.axis('equal')
plt.legend(loc=2)
plt.show()
Film()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末梳杏,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌十性,老刑警劉巖叛溢,帶你破解...
    沈念sama閱讀 223,002評(píng)論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異劲适,居然都是意外死亡楷掉,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評(píng)論 3 400
  • 文/潘曉璐 我一進(jìn)店門霞势,熙熙樓的掌柜王于貴愁眉苦臉地迎上來烹植,“玉大人,你說我怎么就攤上這事愕贡〔莸瘢” “怎么了?”我有些...
    開封第一講書人閱讀 169,787評(píng)論 0 365
  • 文/不壞的土叔 我叫張陵固以,是天一觀的道長(zhǎng)墩虹。 經(jīng)常有香客問我,道長(zhǎng)憨琳,這世上最難降的妖魔是什么诫钓? 我笑而不...
    開封第一講書人閱讀 60,237評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮篙螟,結(jié)果婚禮上菌湃,老公的妹妹穿的比我還像新娘。我一直安慰自己闲擦,他們只是感情好慢味,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,237評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著墅冷,像睡著了一般纯路。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上寞忿,一...
    開封第一講書人閱讀 52,821評(píng)論 1 314
  • 那天驰唬,我揣著相機(jī)與錄音,去河邊找鬼腔彰。 笑死叫编,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的霹抛。 我是一名探鬼主播搓逾,決...
    沈念sama閱讀 41,236評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼杯拐!你這毒婦竟也來了霞篡?” 一聲冷哼從身側(cè)響起世蔗,我...
    開封第一講書人閱讀 40,196評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎朗兵,沒想到半個(gè)月后污淋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡余掖,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,794評(píng)論 3 343
  • 正文 我和宋清朗相戀三年寸爆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盐欺。...
    茶點(diǎn)故事閱讀 40,928評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡赁豆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出找田,到底是詐尸還是另有隱情歌憨,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評(píng)論 5 351
  • 正文 年R本政府宣布墩衙,位于F島的核電站,受9級(jí)特大地震影響甲抖,放射性物質(zhì)發(fā)生泄漏漆改。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,264評(píng)論 3 336
  • 文/蒙蒙 一准谚、第九天 我趴在偏房一處隱蔽的房頂上張望挫剑。 院中可真熱鬧,春花似錦柱衔、人聲如沸樊破。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽哲戚。三九已至,卻和暖如春艾岂,著一層夾襖步出監(jiān)牢的瞬間顺少,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評(píng)論 1 274
  • 我被黑心中介騙來泰國打工王浴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留脆炎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,378評(píng)論 3 379
  • 正文 我出身青樓氓辣,卻偏偏與公主長(zhǎng)得像秒裕,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子钞啸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,937評(píng)論 2 361