简体   繁体   中英

How to use findAll() function in python

hi how can I use findAll() function in python to access this 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. You need a comma after id_='containerTable' .

  2. data-webcallurls_ is not a valid variable name (because of the hyphen) and hence not a valid keyword argument. 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:

     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:

     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.

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"}]'}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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