Python学习的第四天

爬虫学习

路劲表达式
7UVR78T013CI47F(88K`$BC.png

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)
# print(url)
    # 获取站点str类型的响应
    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)
    # 提取目标站的信息
    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)
    #  图书价格
    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 = '当当自营'
    # else:
    #     store = store[0]
    store = '当当自营' if len(store) == 0 else store[0]
    # print(store)
        # 添加每一个商家的图书信息
        book_list.append({
            'title':title,
            'price':price,
            'link':link,
            'store':store
        })

# 按照价格进行排序
book_list.sort(key=lambda x:x['price'])

# 遍历booklist
for book in book_list:
    print(book)
    # 展示价格最低的前10家 柱状图
    # 店铺的名称
    top10_store = [book_list[i] for i in range(10)]
    # x = []
    # for store in top10_store:
    #     x.append(store['store'])
    x = [x['store'] for x in top10_store]
    print(x)
    # 图书的价格
    y = [x['price'] for x in top10_store]
    print(y)
    # plt.bar(x, y)
    plt.barh(x, y)
    plt.show()

# 存储成csv文件


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

实战演示

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_dianying():
    film_list = []
    url = 'https://movie.douban.com/cinema/later/chongqing/'
    print(url)

    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('dianying.html', 'w', encoding='utf-8') as f:
  f.write(html_data)
selector = html.fromstring(html_data)
ul_list = selector.xpath('//div[@id="showing-soon"]/div')
print('即将上映的电影'.format(len(ul_list)))
    for li in ul_list:
      title=li.xpath('./div/h3/a/text()')[0]
      #日期
      date=li.xpath('./div/ul/li[1]/text()')[0]

      type=li.xpath('./div/ul/li[2]/text()')[0]

      country=li.xpath('./div/ul/li[3]/text()')[0]

      number=li.xpath('./div/ul/li[4]/span/text()')[0]
      number=int(number.replace("人想看",""))
      film_list.append({
          'title': title,
          'date': date,
          'type': type,
          'country':country,
          'number':number

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


counts = {}
for movie in film_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)
plt.pie(countrys_count, labels=countrys, autopct='%1.1f%%')
plt.show()

for book in film_list:
    print(book)
        # 展示最多的前5名电影 柱状图
        # 电影的名称
    top5_store = [film_list[i] for i in range(5)]
    # x = []
    # for store in top10_store:
    #     x.append(store['store'])
    x = [x['title'] for x in top5_store]
    print(x)
    # 电影人数
    y = [x['number'] for x in top5_store]
    print(y)
    # plt.bar(x, y)
    plt.barh(x, y)
    plt.show()

spider_dianying()//调用方法

柱状图

(M`J7_B@(%[email protected]

饼图

![4HIAZ4$0[]]KR@HBCC)L7D.png

你可能感兴趣的:(Python学习的第四天)