Python使用Selenium,ValueError:要取消订阅的值太多

2024-10-03 04:37:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在浏览http://m.imdb.com/feature/bornondate我想我的代码以前是可以工作的,但是今天它给了我一个错误:

"ValueError: too many values to unpack" at File "/Users/Desktop/IMDB_BornToday_Scraping.py", line 28, in profession, bestWork = person.split(", ")

(页面每天都在变)这里有什么问题?在

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv

c = csv.writer(open("celebritiesBornToday.csv", "wb"))
driver = webdriver.PhantomJS(executable_path='/Users/Downloads/phantomjs-2.0.0-macosx/bin/phantomjs')
driver.get("http://m.imdb.com/feature/bornondate")

# waiting for posters to load
wait = WebDriverWait(driver, 10)
posters = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "section.posters")))

#giving headings to the columns of the csv file
c.writerow(['name','image','profession','bestWork'])

# extracting the data poster by poster
for a in posters.find_elements_by_css_selector('a.poster'):

    # Fetching the picture of the celebrity
    image = a.find_element_by_tag_name('img').get_attribute('src').split('._V1.')[0] + '._V1_SX214_AL_.jpg'
    # Fetching the profession and bestWork of the celebrity as person
    person = a.find_element_by_css_selector('div.detail').text
    # Splitting person into profession and bestWork
    profession, bestWork = person.split(",")
    # Fetching the name of the celebrity
    name = a.find_element_by_css_selector('span.title').text

    #Printing the Name of the Celebrity
    print "Name of the celebrity: "+name
    #Printing the Image of the Celebrity
    print "Image: "+image
    #Printing the Profession of the Celebrity
    print "Profession: "+profession
    #Printing the BestWork of the Celebrity
    print "BestWork: "+bestWork

    #saving the name, image, profession, bestWork of the celebrity into a csv file
    c.writerow([name,image,profession,bestWork])

Tags: ofcsvthenamefromimageimportby
1条回答
网友
1楼 · 发布于 2024-10-03 04:37:04

问题是,3月16日出生的第一个人的最佳工作头衔包含一个逗号:

Actor, "I, Robot"

这将导致一个ValueError,而其上的拆分已解包,演示:

^{pr2}$

您需要按第一次出现的逗号进行拆分

profession, bestWork = person.split(",", 1)

演示:

>>> profession, bestWork = s.split(',', 1)
>>> profession
'Actor'
>>> bestWork
' "I, Robot"'

相关问题 更多 >