出bug的代码如下:
res = requests.get(url_temp)
bs = BeautifulSoup(res.text,'html.parser')
movie_list = bs.find_all('ol',class_="grid_view")
**for i in movie_list.find_all('li'):**
num = i.find('em',class_='''''').text
name = i.find('span',class_='title').text
grades = i.find('span',class_='rating_num').text
review = i.find('p',class_='quote').text
link = i.find('a')['href']
本来是想爬一下豆瓣top250的电影,结果在爬取每一页的电影信息的时候出了问题,建议我用find()
"ResultSet object has no attribute ‘%s’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute ‘find_all’. You’re probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
但是改成find(‘li’)还是报错,结果变成了建议我用find_all(),这尼玛不是脑子有病。。。。。
上网查了一下也没找到什么可行的办法,他们说让我别用BeautifulSoup了(我就不)
然后想了一下,既然find_all解析出来是类似于列表,那我调用其中的某一个是不是就对应的某一个标签呢??
于是我把代码改了一下:
res = requests.get(url_temp)
bs = BeautifulSoup(res.text,'html.parser')
movie_list = bs.find_all('ol',class_="grid_view")
**for i in movie_list[0].find_all('li'):**
num = i.find('em',class_='''''').text
name = i.find('span',class_='title').text
grades = i.find('span',class_='rating_num').text
review = i.find('p',class_='quote').text
link = i.find('a')['href']
???就好了???bug就解决了
我现在也暂时不知道为什么,还要再查一下,但是万一大家也在查这个bug呢,就先提出了一个解决办法,如果我知道了为什么再来补充一下。
贴上我用来爬电影的代码:
import requests
from bs4 import BeautifulSoup
#序号/电影名/评分/推荐语/链接
url_list = []
num_temp = 0
while num_temp < 250:
url = 'https://movie.douban.com/top250?start='+ str(num_temp) +'&filter='
url_list.append(url)
num_temp = num_temp + 25
for url_temp in url_list:
res = requests.get(url_temp)
bs = BeautifulSoup(res.text,'html.parser')
movie_list = bs.find_all('ol',class_="grid_view")
for i in movie_list[0].find_all('li'):
try:
num = i.find('em',class_='''''').text
name = i.find('span',class_='title').text
grades = i.find('span',class_='rating_num').text
review = i.find('p',class_='quote').text
link = i.find('a')['href']
except(AttributeError):#因为有的电影没有推荐语,会报错
review = ' \n\n'
print('序号:'+ num + '\n 电影名:' + name + ' 评分:' + grades + ' 推荐语:' + review + ' 电影连接:' + link)