2017-10-18 48 views
3

添加屬性,這裏是我的HTML結構湊:BeautifulSoup - 對結果集

<div class='schedule-lists'> 
    <ul> 
     <li>...</li> 
      <ul> 
       <li>...</li> 
        <ul class='showtime-lists'> 
         <li>...</li> 
          <li><a auditype="N" cinema="0100" href="javascript:void(0);" >12:45</a></li> 
         <li>...</li> -- (same structured as above) 
         <li>...</li> -- (same structured as above) 

     <li>...</li> -- (same structured as above) 
     <li>...</li> -- (same structured as above) 

這裏是我的代碼:

from requests import get 
from bs4 import BeautifulSoup 
response = get('www.example.com') 
response_html = BeautifulSoup(response.text, 'html.parser') 
containers = response_html.find_all('ul', class_='showtime-lists') 
#print(containers) 
[<ul class="showtime-lists"> 
<li><a auditype="N" cinema="0100" href="javascript:void(0);" >12:45</a></li> 

我怎麼能在我的結果集容器添加屬性?比如增加movietitle =「洛根」,所以它變成了:

<li><a movietitle="Logan" auditype="N" cinema="0100" href="javascript:void(0);" >12:45</a></li> 

我最好的試驗是使用.append方法,但它可以做到的,因爲像一本字典的ResultSet的行爲

回答

0

你可以試試這個:

... 
a = find_all('a') 
i = 0 
for tag in a: 
    a[i]['movietitle'] = 'Logan' 
    i += 1 
print str(a)