targets2 = soup.find("div", class_="hd").select('span:nth-of-type(2)')
只能找到一个"div", class_="hd"的第二个span,但是用了find_all又不能用select。
targets2 = soup.find_all("div", class_="hd")
只能获取第一个span
targets = soup.find_all("span", class_="title")
又会同时获取多个class_="title"的电影名
想要爬取所有豆瓣top250电影第二个电影名
先获取所有class='hd'的div保存到列表中,然后遍历列表中每一项获取这一项div的第二个span
targets2 = soup.find_all("div", class_="hd")
for item in targets2:
print(item.select('span:nth-of-type(2)'))
find_all定位div标签,然后遍历这个定位对象,每次遍历再定位span即可