网页抓取问题,无法点击元素

时间:2021-03-13 10:04:12

标签: python

我试图循环浏览这个网站并按下每个元素/按钮,但我总是收到非类型错误... https://www.coop.se/globalt-sok/?query=coop&category=stores&page=1 enter image description here 这是我试图点击每个元素/绿色按钮的网站 enter image description here

我的代码在这里:

buttons = soup.find_all('div', class_='Grid-cell js-storeResult u- 
cursorPointer')


if len(buttons) == 0:
    print("----------------------------------------")
    print("----------------------------------------")
    print("Jumped over this page",driver.current_url)
    print("----------------------------------------")
    print("----------------------------------------")
    continue
else:


 for button in buttons:
        Store = button.find("div").attrs["data-name"]

        test = button.find('div', {'class':'Button Button-- 
               greenButton--small Button--radius'})
        test.click()

4 个答案:

答案 0 :(得分:0)

错误消息表示 testNone,可能是因为找不到按钮。

当您在 Firefox 中打开网站时,右键单击绿色按钮并选择 Inspect Element(Q),您会看到该类实际上是 Button Button--green按钮--半径

答案 1 :(得分:0)

使用 try 很有用,除了在使用 find 或 click 时捕获任何异常。 joao 评论已经错误的类名也意味着无法找到按钮:

for button in buttons:
    Store = button.find("div").attrs["data-name"]
    try:
        test = button.find('div', {'class':'Button Button--green Button--radius'})
        test.click()
    except Exception as e:
        print(e)

答案 2 :(得分:0)

刚刚检查了 bs4.element.Tag 的所有方法,没有 click() 方法:

<块引用>

['bool', 'call', 'class', 'contains', '<强>复制', 'delattr'、'delitem'、'dict'、'dir'、'doc< /强>', 'eq'、'格式'、'ge'、'getattr'、'getattribute< /强>', 'getitem'、'gt'、'hash'、'init'、'init_subclass< /强>', 'iter'、'le'、'len'、'lt'、'模块< /strong>', 'ne', 'new'、'reduce'、'reduce_ex'、'repr'、'setattr< /强>', 'setitem'、'sizeof'、'str'、'subclasshook'、 'unicode'、'weakref'、'_all_strings'、'_find_all'、 '_find_one', '_is_xml', '_lastRecursiveChild', '_last_descendant', '_should_pretty_print', 'append', 'attrs', 'can_be_empty_element', 'cdata_list_attributes', 'childGenerator', 'children', 'clear', “内容”、“解码”、“解码内容”、“分解”、“分解”、 “后代”、“编码”、“编码内容”、“扩展”、“提取”、 'fetchNextSiblings', 'fetchParents', 'fetchPrevious', 'fetchPreviousSiblings', 'find', 'findAll', 'findAllNext', 'findAllPrevious', 'findChild', 'findChildren', 'findNext', 'findNextSibling', 'findNextSiblings', 'findParent', 'findParents', 'findPrevious', 'findPreviousSibling', 'findPreviousSiblings', 'find_all', 'find_all_next', 'find_all_previous', 'find_next', 'find_next_sibling', 'find_next_siblings', 'find_parent', 'find_parents', 'find_previous', 'find_previous_sibling', 'find_previous_siblings', 'format_string', 'formatter_for_name', 'get', 'getText', 'get_attribute_list', 'get_text', 'has_attr', 'has_key', 'hidden', 'index', 'insert', 'insert_after', 'insert_before', 'isSelfClosing', 'is_empty_element', 'known_xml', 'name', 'namespace', 'next', 'nextGenerator', 'nextSibling', 'nextSiblingGenerator', 'next_element', 'next_elements', 'next_sibling', 'next_siblings', 'parent', 'parentGenerator', '父母','解析器类','解析器类','前缀', 'preserve_whitespace_tags', 'prettify', 'previous', 'previousGenerator', 'previousSibling', 'previousSiblingGenerator', 'previous_element', 'previous_elements', 'previous_sibling', 'previous_siblings', 'recursiveChildGenerator', 'renderContents', 'replaceWith', 'replaceWithChildren', 'replace_with', 'replace_with_children', 'select', 'select_one', 'setup', 'smooth', 'string', 'strings', 'stripped_strings', 'text', 'unwrap', 'wrap']

这就是出现错误的原因:

<块引用>

TypeError: 'NoneType' 对象不可调用

此外,BeautifulSoup是一个HTML解析器,它不能操作网页,你应该像selenium一样使用smth

答案 3 :(得分:0)

您需要尝试通过不同的方法找到该按钮。尝试使用不同优先级的组合,而不是具有特定属性名称的 div 外观。另外,您应该更喜欢使用 selenium 进行网页抓取,因为这为您提供了多种选择来顺利定位元素。尝试使用类名或其中的一部分进行搜索