python获取豆瓣电影基本电影信息(新手向)

import requests
from bs4 import BeautifulSoup
import os

id = input("请输入豆瓣电影的ID:")
url = "https://movie.douban.com/subject/"+ id +"/"

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}

html_code = requests.get(url,headers=headers)

soup = BeautifulSoup(html_code.text,"html.parser")

name = soup.find("span",attrs={"property":"v:itemreviewed"}).get_text()
pf = soup.find("strong",attrs={"property":"v:average"}).get_text()
dy = soup.find("a",attrs={"rel":"v:directedBy"}).get_text()
jqjj = soup.find("span",attrs={"property":"v:summary"}).get_text()

print("电影名称:",name)
print("豆瓣评分:",pf)
print("导演:",dy)
print("剧情简介:",jqjj.replace("\n","").replace(" ","").replace("  ","").strip())

os.system("pause")

 

你可能感兴趣的:(爬虫,python)