得到'div‘元素,但同样的方式,不要得到'a’元素。
import requests
from bs4 import BeautifulSoup as bf
# link :
url = "https://www.imdb.com/search/name/?gender=male,female&ref_=nv_tp_cel"
Response = requests.get(url)
soup = bf(Response.text,'lxml')
div = soup.find_all('div', class_ = "lister-item-content")
a_tag = div.find_all('a')
print(a_tag)我想打印'div‘元素中的所有'a’元素。
发布于 2019-10-27 07:16:22
遍历每个div元素,然后使用div.findChildren遍历每个a子元素
import requests
from bs4 import BeautifulSoup as bf
# link :
url = "https://www.imdb.com/search/name/?gender=male,female&ref_=nv_tp_cel"
Response = requests.get(url)
soup = bf(Response.text,'lxml')
divs = soup.find_all('div', class_ = "lister-item-content")
for div in divs:
a_tags = div.findChildren('a')
for a_tag in a_tags:
print (a_tag)它会打印出如下内容:
...史密斯搏击俱乐部,金钱球,海洋的十一,凡妮莎,玛尔西,岩石
https://stackoverflow.com/questions/58577237
复制相似问题