python 爬虫实战(一)爬取豆瓣图书top250

时间:2022-06-02 17:45:13
import requests
from lxml import etree

with open('booktop250.txt','w',encoding='utf-8') as f:
    for i in range(0,226,25):
        url = 'https://book.douban.com/top250?start={}'.format(i)
        data = requests.get(url).text

        page = etree.HTML(data)
        file = page.xpath('//*[@id="content"]/div/div[1]/div/table')
        for div in file:
            title = div.xpath('./tr/td[2]/div[1]/a/@title')
            pf = div.xpath('./tr/td[2]/div[2]/span[2]/text()')
            words = div.xpath('./tr/td[2]/p[2]/span/text()')

            f.write("\n""{}{}{}".format(title,pf,words))

print("ALL IS OVER!")