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

标签 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

请帮忙。

最佳答案

我想这就是你需要的:

[
('%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 != '전체']

请注意 % 也是 Python 中的“模”运算符,您不能将其应用于元组

关于python - 使用 Selenium Python 将数据收集为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41792822/

相关文章:

python - PyArrow:如何使用新的文件系统接口(interface)将文件从本地复制到远程?

javascript - 按类型单击 Selenium 中的元素

html - 如何在不影响网页的情况下将文本向左移动?

javascript - Ajax 获取 XML 数据类型比 Json 有什么优势吗?

JavaScript 和 php 一段时间后停止工作,只有在重命名一些变量时才有效

java - 如何在 Selenium WebDriver 中将 Dropdown 与 ng-model 一起使用?

firefox - 在 Linux 上的 Jenkins 下运行的 WebDriver 测试中出现 NotConnectedException 错误

python - os.pathconf() 键及其含义

python - Python 中的 Connect() 函数在 Linux 中不起作用

python - 如果python脚本包含#!/usr/bin/python3,是否需要在外部指定python解释器?