简体   繁体   English

如何在 python 中使用 findAll() function

[英]How to use findAll() function in python

hi how can I use findAll() function in python to access this div?嗨,我如何在 python 中使用 findAll() function 来访问这个 div?

<div id="containerTable" data-webcallurls="[{&quot;url&quot;:&quot;https://finance-services.msn.com/Market.svc/ChartAndQuotes?symbols=142.1.MRC.PHS&amp;chartType=1d&amp;isETF=false&amp;iseod=False&amp;lang=en-PH&amp;isCS=true&amp;isVol=true&quot;}]"><ul>

I tried this but I'm getting syntax error:我试过这个,但我收到语法错误:

containers2 = soup.findAll("div", id_='containerTable', data-webcallurls_='[{"url":"https://finance-services.msn.com/Market.svc/ChartAndQuotes?symbols=142.1.MRC.PHS&chartType=1d&isETF=false&iseod=False&lang=en-PH&isCS=true&isVol=true"}]')
  1. The edit by user @r0ei fixes one of the syntax errors.用户@r0ei 的编辑修复了其中一个语法错误。 You need a comma after id_='containerTable' .id_='containerTable'之后需要一个逗号。

  2. data-webcallurls_ is not a valid variable name (because of the hyphen) and hence not a valid keyword argument. data-webcallurls_不是有效的变量名(因为连字符),因此不是有效的关键字参数。 The documentation provides an example of exactly this : 该文档提供了一个示例

    Some attributes, like the data-* attributes in HTML 5, have names that can't be used as the names of keyword arguments:某些属性,例如 HTML 5 中的 data-* 属性,其名称不能用作关键字 arguments 的名称:

     data_soup = BeautifulSoup('<div data-foo="value">foo.</div>') data_soup:find_all(data-foo="value") # SyntaxError: keyword can't be an expression

    You can use these attributes in searches by putting them into a dictionary and passing the dictionary into find_all() as the attrs argument:您可以通过将这些属性放入字典并将字典作为 attrs 参数传递给 find_all() 来在搜索中使用这些属性:

     data_soup.find_all(attrs={"data-foo": "value"}) # [<div data-foo="value">foo!</div>]
  3. id should be passed in as id=... , not id_=.... - you don't need the trailing underscore. id应该作为id=...传入,而不是id_=.... - 你不需要结尾的下划线。

Finally, your call should look like this:最后,您的调用应如下所示:

containers2 = soup.find_all("div",
                            id='containerTable',
                            attrs={'data-webcallurls_': '[{"url":"https://finance-services.msn.com/Market.svc/ChartAndQuotes?symbols=142.1.MRC.PHS&chartType=1d&isETF=false&iseod=False&lang=en-PH&isCS=true&isVol=true"}]'}
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM