美丽的汤图像刮刀问题

时间:2015-10-11 15:52:41

标签: python selenium selenium-webdriver web-scraping beautifulsoup

我得到以下追溯:

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)

我刚开始,所以我很感激有关如何改进代码的任何提示。 三江源

2 个答案:

答案 0 :(得分:1)

driver.get()在浏览器中加载网页并返回None,这会使searched变量的值为None

你可能想要取代.page_source

soup = BeautifulSoup(driver.page_source, "lxml")

这里还有两点:

  • 您实际上并不需要BeautifulSoup - 您可以使用selenium找到所需的图片,例如driver.find_elements_by_tag_name()
  • 我尚未测试您的代码,但我认为您需要添加额外的Explicit Waits才能使selenium等待页面加载

答案 1 :(得分:0)

searchedNone。显然,您使用的网址无效。