最简单的python爬虫

没有任何参考价值,仅供个人记录

最最简单的python爬虫,爬取豆瓣的网页并将电影的网页链接和电影名字保存在excel中

import requests
from pyquery import PyQuery as pq
from openpyxl import load_workbook

def insert_excel(excel,title,rating,link,rows):
    sheet = excel.active
    sheet.cell(row=rows,column=1).value = title
    sheet.cell(row=rows, column=2).value = rating
    sheet.cell(row=rows, column=3).value = link

def getDoubanMovie():
    excel = load_workbook('test.xlsx')
    rows = 1
    for page in range(0,250,25):
        urls = 'https://movie.douban.com/top250?start=%d&filter='%(page)
        content = requests.get(urls).content
        for movie in pq(content).find('.item'):
            title = pq(movie).find('.title').html()
            rating = pq(movie).find('.rating_num').text()
            link = pq(movie).find('a').attr('href')
            print(title,rating,link,sep='\t\t')
            insert_excel(excel,title,rating,link,rows)
            rows = rows + 1
    excel.save('test.xlsx')

if __name__ == '__main__':
    getDoubanMovie()

 

posted @ 2018-01-22 16:55  KaShing  阅读(274)  评论(0编辑  收藏  举报