爬取豆瓣图书
最近在学习爬虫的基础使用,发现最好爬的网站就是豆瓣了,随随便便都是可以爬到数据。
下面就是使用Python中间的requests模块实现豆瓣图书排行榜的爬取:
import requests
from lxml import etree
import urllib3
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
}
url = 'https://book.douban.com/'
def get_url(url):
dict_book = {}
response = requests.get(url, verify=False).text
response = etree.HTML(response)
for i in range(0,10):
ret = response.xpath('//*[@id="content"]/div/div[1]/div[4]/div[2]/ul/li['+ str(i+1)+']/div[2]/h4/a