python爬取豆瓣图书数据

from lxml import etree
import requests
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'
}
with open('douban.txt','a',encoding='utf-8') as file:
    url='https://book.douban.com/top250'
    for i in range(10):
        resp=requests.get(url,params={'start':i*25},headers=headers)
        html=etree.HTML(resp.text)
        tables=html.xpath('//div[@id="content"]/div/div[1]/div/table')
        for item in tables:
            book_url=item.xpath('./tr/td[2]/div/a/@href')[0]
            book_name=item.xpath('./tr/td[2]/div/a/@title')[0]
            book_author=item.xpath('./tr/td[2]/p[1]')[0].text
            book_score=item.xpath('./tr/td[2]/div[2]/span[2]')[0].text
            rate_count=item.xpath('./tr/td[2]/div[2]/span[3]')[0].text.replace('(',"").replace(')','')
            scribe=item.xpath('./tr/td[2]/p[last()]/span')
            if len(scribe)==0:
                file.write('{},{},{},{},{}\n'.format(book_url,book_name,book_author,book_score,rate_count))
            else:
                file.write('{},{},{},{},{},{}\n'.format(book_url, book_name, book_author, book_score, rate_count,scribe[0].text))


            print(book_author)




你可能感兴趣的:(python爬取豆瓣图书数据)