Python 爬取豆瓣网电影和存取CSV文件

import requests
import csv
from bs4 import BeautifulSoup
headers={“user-agent”:“Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36”,“Referer”:“https://movie.douban.com/”}
proxy={“http”:“218.60.8.83:3129”,“http”:“113.78.67.113:9797”}
res=requests.get(“https://movie.douban.com/cinema/nowplaying/shenzhen/”,headers=headers,proxies=proxy)
res.encoding=“utf-8”
soup=BeautifulSoup(res.text,“html.parser”)
Movies=[]
for movie in soup.select(".lists > li"):
Movie_Name=movie[“data-title”]
#Movie_Score=movie[“data-score”]
#Movie_Release=movie[“data-release”]
Movie_Duration=movie[“data-duration”]
Movie_Region=movie[“data-region”]
Movies.append([Movie_Name,Movie_Duration,Movie_Region])
print(Movie_Name,Movie_Duration,Movie_Region)
with open(“douban.csv”,“w”,newline="") as fp:
write_flie = csv.writer(fp)
head = [“电影名称”,“电影时长”,“影片地区”]
write_flie.writerow(head)
for rows in Movies:
write_flie.writerow(rows)
fp.close()
###################################################################################################
Python 爬取豆瓣网电影和存取CSV文件_第1张图片
以上代码,均测试成功;

  • **
    • 代码里面有两部电影,没有data-score 这个属性,结果就是死活不能取出data-score的值,所以我注释掉了评分的字段,希望高手能指点!

    **

    Python 爬取豆瓣网电影和存取CSV文件_第2张图片Python 爬取豆瓣网电影和存取CSV文件_第3张图片

    你可能感兴趣的:(Python 爬取豆瓣网电影和存取CSV文件)