import requests from bs4 import BeautifulSoup url = 'https://movie.douban.com/subject/30228394/' header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'} response = requests.get(url=url, headers=header) soup = BeautifulSoup(response.text, 'html.parser') tv_infor = {} # 1.获取电视剧名称 name = soup.find(property="v:itemreviewed").string # 根据属性property="v: itemreviewed"查找 tv_infor['name'] = name # 将电影名称加到字典tv_infor中 # 2.获取导演 director = soup.find(rel="v: directedBy").string # 根据属性re1="v:directedBy“查找 tv_infor['director'] = director # 3.获取编剧 soup_list = soup. findAll(class_="attrs")[1].findAll('a') writers = [elem. string for elem in soup_list] tv_infor['writers'] = writers # 4.获取演员 soup_list = soup. findAll(rel="v:starring") actors = [elem. string for elem in soup_list] tv_infor['actors'] = actors # 5.获取类型 soup_list = soup. findAll(property="v: genre") tv_type = [elem. string for elem in soup_list] tv_infor['type'] = tv_type # 6.首播时间 release_date = soup.find(property="v: initialReleaseDate").string tv_infor['release_date'] = release_date # 7.豆瓣评分 rating = soup.find(property="v: average").string tv_infor['rating'] = rating # 8.参评人数 votes = soup.find(property="v: votes").string tv_infor['votes'] = votes print("电视剧《觉醒年代》相关信息如下:") for key, value in tv_infor.items(): print(key, ":", value)运行报错
时间: 2024-03-29 12:38:51 浏览: 196
https://ljgk.envsc.cn/爬虫结果
这段代码是Python 2.x的语法,但在Python 3.x中会出现语法错误。你需要将所有的print语句修改为函数调用的格式,即在print后添加括号,像这样:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/subject/30228394/'
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'}
response = requests.get(url=url, headers=header)
soup = BeautifulSoup(response.text, 'html.parser')
tv_infor = {}
# 1.获取电视剧名称
name = soup.find(property="v:itemreviewed").string
tv_infor['name'] = name
# 2.获取导演
director = soup.find(rel="v: directedBy").string
tv_infor['director'] = director
# 3.获取编剧
soup_list = soup.findAll(class_="attrs")[1].findAll('a')
writers = [elem.string for elem in soup_list]
tv_infor['writers'] = writers
# 4.获取演员
soup_list = soup.findAll(rel="v:starring")
actors = [elem.string for elem in soup_list]
tv_infor['actors'] = actors
# 5.获取类型
soup_list = soup.findAll(property="v:genre")
tv_type = [elem.string for elem in soup_list]
tv_infor['type'] = tv_type
# 6.首播时间
release_date = soup.find(property="v:initialReleaseDate").string
tv_infor['release_date'] = release_date
# 7.豆瓣评分
rating = soup.find(property="v:average").string
tv_infor['rating'] = rating
# 8.参评人数
votes = soup.find(property="v:votes").string
tv_infor['votes'] = votes
print("电视剧《觉醒年代》相关信息如下:")
for key, value in tv_infor.items():
print(key, ":", value)
```
这样就可以正常运行了。
阅读全文