# 目標(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)
# 圖書價格
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)
-- 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
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 = []
# 目標(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)
# 圖書價格
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)
# 添加每一個商家的圖書信息
book_list.append({
'title':title,
'price':price,
'link':link,
'store':store
})
# 按照價格進(jìn)行排序
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')
爬蟲
1寞酿、爬蟲基礎(chǔ)
1.1匠襟、獲取網(wǎng)址
url='https://www.baidu.com'
response=requests.get(url)
1.2钝侠、獲取str類型的響應(yīng)
print(response.text)
1.3、獲取bytes類型的響應(yīng)
print(response.content)
1.4酸舍、獲取響應(yīng)頭
print(response.headers)
1.5帅韧、獲取狀態(tài)碼
print(response.status_code)
1.6、返回200成功啃勉,400未找到忽舟,500服務(wù)器錯誤
1.7、響應(yīng)頭用以偽裝成瀏覽器
#沒有添加響應(yīng)頭
# resp=requests.get('https://www.zhihu.com/')
# print(resp.status_code)
#運(yùn)行返回400
#使用字典定義請求頭
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}
resp=requests.get('https://pvp.qq.com/')
print(resp.status_code)
#運(yùn)行返回200
2、靜態(tài)網(wǎng)頁爬蟲
2.1萧诫、導(dǎo)入lxml庫
from lxml import html
2.2、打開并讀取本地html文件
with open('./index.html','r',encoding='utf-8') as f:
html_data=f.read()
print(html_data)
2.3枝嘶、解析html文件帘饶,獲取selector對象
selector =html.fromstring(html_data)
#要獲取標(biāo)簽內(nèi)容,末尾要添加text()
h1=selector.xpath('/html/body/h1/text()')
print(h1[0])
2.4群扶、//表示可以代表任意位置出發(fā)
#//標(biāo)簽1[@屬性=屬性值]/標(biāo)簽2[@屬性=屬性值]..../text()
a=selector.xpath('//div[@id="container"]/a/text()')
print(a)