使用Selenium Python将数据收集为元组

时间:2017-01-22 15:34:45

标签: python html selenium beautifulsoup web-crawler

我使用Selenium Python 3.6将数据收集为元组时遇到问题。这是我要收集数据的页面(http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I) 我想在页面上部的搜索菜单中收集“制造商(制造商)”数据。

enter image description here

我使用selenium webdrivet设置虚拟页面并使用此代码收集并选择第一个下拉菜单列表:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException

from bs4 import BeautifulSoup
from time import sleep


link = 'http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I'
driver = webdriver.PhantomJS()
driver.set_window_size(1920, 1080)
driver.get(link)
sleep(.75)

soup = BeautifulSoup(driver.page_source, "html.parser", from_encoding='utf-8')

manufacturers = [
    ('%s', '%s') % (o.text, o.get_attribute('href'))
    for o
    in driver.find_elements_by_css_selector("#layer_maker ul.list li a")
    if o.text != '전체']

for manufacturer in manufacturers:
    driver.execute_script("o.get_attribute('href')")

而且,这是我的错误信息:

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/chongwonshin/PycharmProjects/Crawler_test/dump.py
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/bs4/__init__.py:146: UserWarning: You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.
  warnings.warn("You provided Unicode markup but also provided a value for from_encoding. Your from_encoding will be ignored.")
Traceback (most recent call last):
  File "/Users/chongwonshin/PycharmProjects/Crawler_test/dump.py", line 23, in <module>
    in driver.find_elements_by_css_selector("#layer_maker ul.list li a")
  File "/Users/chongwonshin/PycharmProjects/Crawler_test/dump.py", line 24, in <listcomp>
    if o.text != '전체']
TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple'

Process finished with exit code 1

请帮忙。

1 个答案:

答案 0 :(得分:1)

我想这就是你需要的:

[
('%s' % o.text, '%s' % o.get_attribute('href'))
for o
in driver.find_elements_by_css_selector("#layer_maker ul.list li a")
if o.text != '전체']

或只是

[
(o.text, o.get_attribute('href'))
for o
in driver.find_elements_by_css_selector("#layer_maker ul.list li a")
if o.text != '전체']

请注意%也是&#34;模数&#34; Python中的运算符,您无法应用于元组