2017-06-06 5 views
1

のpythonウェブスクラップ

TRに記載されている各学生は、各学生のために1だけインクリメント独自のIDタグを有しています。

例:1234-1、1234から2、1234から3など。

私は、出力は最初のTDを提供しても1ずつカウント変数をインクリメントすることにより、IDに追加しようとしましたが、すべてではないしていますtdのpythonに新しいともウェブスクレイピング、これが機能しない理由がわからない

イム。すべてのヘルプずっと

import csv 
import requests 
from bs4 import BeautifulSoup 

url = '' # Has been left blank for a reason 
response = requests.get(url) 
html = response.content 

count = 1 

print ('-' * 30) 

soup = BeautifulSoup(html, "html.parser") 
table = soup.find('tr', attrs={'id': '1234-' + str(count)}) 

list_of_cells = [] 

while True: 
    for cell in table.findAll('td'): 
     text = cell.text.replace('\xa0', '') 
     list_of_cells.append(text) 
    list_of_cells.append(list_of_cells) 

    student_name = list_of_cells[0] 
    agent_id = list_of_cells[3].replace('-', '') 

    total_hrs = list_of_cells[14] 
    total_inc = list_of_cells[15] 

    count += 1 

    print (student_name, "| ", total_hrs, " ", total_inc) 
else: 
    print('Done') 

テーブル内のTRの例をいただければ幸いです。..

<tr height="17" id="1234-1" style="height:12.75pt;display:none"> 
    <td class="xl243045" height="17" style="height:12.75pt;border-top:none"> 
    <a href="48701">Student Name</a> 
    </td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
    <td style="border-top:none;border-left:none">stuff</td> 
</tr> 

答えて

1

美しいスープを使用すると、正規表現で選択できます。

import re 

# if you copy and paste this be wary of the "-" it doesn't appear to be a standard "-" on a US keyboard. Make it match whatever is in the html 
students = soup.find_all("tr",id=re.compile(r'\d{4}-\d+')) 
for student in students: 
    cells = student.find_all("td") 
    student_name = cells[0].find('a').text 
    total_hrs = cells[14].text 
    print("{0}|{1}".format(student_name, total_hrs)) 

しかし、私はあなたのテーブルを推測しているが、おそらくちょうど学生の行で満たされている。だから、この何かを行うことができます。それは、これはより多くの意味を作るとフォローしやすいかもしれ続いている場合:

#access the actual table holding the rows not the row itself -- notice the parent 
table = soup.find('tr', attrs={'id': '1234-1'}).parent 

# iterate over each of the rows (students) 
for row in table.find_all("tr"): 
    cells = row.find_all("td") 
    student_name = cells[0].find('a').text 
    total_hrs = cells[14].text 
    print("{0}|{1}".format(student_name, total_hrs)) 

ところで、テーブルにある学生証に頼ることは最高のアイデアではないかもしれません。 学生は通常変更されます。特定の生徒IDをテーブルに入れることに頼るのではなく、生徒を保持するテーブルを特定するものを見つけることは、はるかに良い考えです。

0

あなたがcountを増やすループ内であることが必要table = soup.find('tr', attrs={'id': '1234-' + str(count)})ライン。

count = 1 

print ('-' * 30) 

soup = BeautifulSoup(html, "html.parser") 

list_of_cells = [] 

while True: 
    table = soup.find('tr', attrs={'id': '1234-' + str(count)}) 
    for cell in table.findAll('td'): 
     text = cell.text.replace('\xa0', '') 
     list_of_cells.append(text) 
    list_of_cells.append(list_of_cells) 

    student_name = list_of_cells[0] 
    agent_id = list_of_cells[3].replace('-', '') 

    total_hrs = list_of_cells[14] 
    total_inc = list_of_cells[15] 

    count += 1 

    print (student_name, "| ", total_hrs, " ", total_inc) 
else: 
    print('Done') 
+0

ありがとうございます。これにより、それが起こっていた無限ループが停止しました。しかし、プラス1の増分が働いていないようです。最初の学生情報のみを提供します。 – titant3ch

関連する問題