我得到以下追溯:
Traceback (most recent call last):
File "/home/ro/image_scrape_test.py", line 20, in <module>
soup = BeautifulSoup(searched, "lxml")
File "/usr/local/lib/python3.4/dist-packages/bs4/__init__.py", line 176, in __init__
elif len(markup) <= 256:
TypeError: object of type 'NoneType' has no len()
到目前为止,这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import urllib
#searches google images
driver = webdriver.Firefox()
google_images = ("https://www.google.com/search?site=imghp&tbm=isch source=hp&biw=1366&bih=648&q=")
search_term = input("what is your search term")
searched = driver.get("{0}{1}".format(google_images, search_term))
def savepic(url):
uri = ("/home/ro/image scrape/images/download.jpg")
if url != "":
urllib.urlretrieve(url, uri)
soup = BeautifulSoup(searched, "lxml")
soup1 = soup.content
images = soup1.find_all("a")
for image in images:
savepic(image)
我刚开始,所以我很感激有关如何改进代码的任何提示。 三江源
答案 0 :(得分:1)
driver.get()
在浏览器中加载网页并返回None
,这会使searched
变量的值为None
。
你可能想要取代.page_source
:
soup = BeautifulSoup(driver.page_source, "lxml")
这里还有两点:
BeautifulSoup
- 您可以使用selenium
找到所需的图片,例如driver.find_elements_by_tag_name()
selenium
等待页面加载答案 1 :(得分:0)
searched
是None
。显然,您使用的网址无效。