其他分享
首页 > 其他分享> > selenium爬取豆瓣电影 保存CSV格式

selenium爬取豆瓣电影 保存CSV格式

作者:互联网

selenium爬取豆瓣电影 保存CSV格式

from selenium import webdriver
import csv
from multiprocessing import Pool

fp = open("电影.csv",'a')
write = csv.writer(fp)
write.writerow(['电影名称','评分','评论人数','代表名言','电影信息'])
brower = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\Chromedriver.exe')
def get(URL):
    try:
        brower.get(URL)

        title = brower.find_elements_by_css_selector('div.hd')
        pingfeng = brower.find_elements_by_css_selector('span.rating_num')
        pinglunrenshu = brower.find_elements_by_css_selector('div.star') # 评分加评论人数
        daiiaominyan = brower.find_elements_by_css_selector('span.inq')
        movieino = brower.find_elements_by_css_selector('div.bd')
        for titl,pingfen,pinglunrensh,daiiaominya,mov in zip(title,pingfeng,pinglunrenshu,daiiaominyan,movieino):
            write.writerow([titl.text,pingfen.text,pinglunrensh.text[3:],daiiaominya.text,mov.text])



    except UnicodeEncodeError:
        pass

if __name__ == '__main__':
    

    url_list = ['https://movie.douban.com/top250?start={0}&filter='.format(i*25) for i in range(10)]
    for url in url_list:
        get(url
            )
        print("爬取成功")

标签:elements,text,selenium,selector,爬取,brower,CSV,find,css
来源: https://blog.csdn.net/weixin_51020254/article/details/115439357