簡體   English   中英

用漂亮的湯抓取Python數據

[英]Python data scraping with beautiful soup

大家好,我是Python新手。 請讓我知道如何使用漂亮的湯從下面的代碼節中抓取數據。

<Td class=cell>
<br>
<blockquote>
<p><B>Question:</b> Which is the world's leading egg-producing         country?</p>
<p><ol><li><label for="q1824-1"><input type=radio id="q1824-1" name=q1824  onClick="check_answer('q1824correct','q1824incorrect','1','1');">China
</label><br><li><label for="q1824-2"><input type=radio id="q1824-2"  name=q1824   onClick="check_answer('q1824correct','q1824incorrect','2','1');">India
</label><br><li><label for="q1824-3"><input type=radio id="q1824-3" name=q1824 onClick="check_answer('q1824correct','q1824incorrect','3','1');">Japan
</label><br><li><label for="q1824-4"><input type=radio id="q1824-4" name=q1824  onClick="check_answer('q1824correct','q1824incorrect','4','1');">Malaysia</label><br></ol></p>

像這樣的輸出

問題:哪個是世界領先的產蛋國?

  1. 中國
  2. 印度
  3. 日本
  4. 馬來西亞

很難確定您到底想要什么,因為您的問題(目前存在)定義得很不明確。

通常,在BeautifulSoup中抓取任何HTML的樣板如下:

response = urllib2.urlopen(url)
html_doc = response.read()
soup = BeautifulSoup(html_doc, 'html.parser')

然后,您可以根據不同的標准從HTML中提取元素,如下所示:

# this would match the top-level element in your snippet
# i.e. <td class="cell">
td_element = soup.find("td", _class="cell")

# this would match all of the <label> elements in your snippet
# (so you'll get a list as your result)
labels = soup.findAll("label")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM