如何解决有什么方法可以获取完整的BeautifulSoup元素?
因此,我一直在尝试构建一个小的脚本,该脚本可以从网站上下载每个脚本,但是遇到了一个问题。当我搜索BeautifulSoup元素时,无法从该元素中获取所有div标签。
这是我的代码:
#! python3
from selenium import webdriver
import bs4
import requests
import time
import os
import re
url = "https://www.flickr.com/"
#os.makedirs("downloaded_photos",exist_ok=True)
while True:
search_input = input("Please insert here what kind of photos would you like to donwload: ")
if search_input == "":
print("")
print("Please don't leave this space blank!")
print("")
time.sleep(1)
continue
else:
print("Searching...")
driver = webdriver.Chrome()
driver.get(url + "search/?text=" + search_input)
res = requests.get(url + "search/?text=" + search_input)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text,"lxml")
all_the_imgs = soup.find(
"div",class_="main search-photos-results")
result = re.findall(r'\.jpg',str(all_the_imgs))
print(len(result))
driver.close()
break
所以我的问题是,是否有任何方法可以从所选元素中获取所有div标签
解决方法
听起来您只需要在find_all
元素上使用search-photo-results
all_the_imgs = soup.find_all(
"div",class_="main search-photos-results",)
我们可以将硒regex
的输出作为关键字,将map
返回的列表附加到名为current_url
的字典中。
map = {}
all_images = re.findall(r'\.jpg',str(all_the_images))
map[driver.current_url] = all_images
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。