擅长:python、mysql、java
<p>下面是一个完整的工作示例,<code>data</code>列表将包含您想要的所有内容,而没有杂音(空字符串等)</p>
<pre><code>import requests
from bs4 import BeautifulSoup
response = requests.get('http://www.alilauro.it').text
bs = BeautifulSoup(response)
data = []
# I don't want to scrape the headers, so I'm slicing the list, emitting the first element
no_header = list(bs.select('#partenze tr'))[1:]
for tr in no_header:
td = tr.select('td')
data.append({
'ORA':td[0].text,
'PARTENZA DA':td[1].text,
'ARRIVO A':td[2].text,
'ACQUISTA':td[4].select('a')[0].attrs['href']
})
print(data)
</code></pre>
<p>注意事项:</p>
<ul>
<li>我使用<code>requests</code>库发出http请求,您可以使用任何您想要的东西</li>
<li>我使用css选择器,使用bs的内置<code>select</code>只是个人选择</li>
</ul>