python爬虫実戦(一)豆弁図書top 250を這い出す

743 ワード

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("
""{}{}{}".format(title,pf,words)) print("ALL IS OVER!")