【发布时间】:2018-06-11 22:36:57
【问题描述】:
以下是我用于读取表格并将其插入 csv 文件的代码。但它只读取标题和第一行。
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import urllib2
import csv
import MySQLdb
import itertools
import time
wiki = "http://10.202.215.24:8081/nmediation/cdot_ces_status_xx.jsp?userName=RJCADMIN"
time.sleep(50)
header = {'User-Agent': 'Mozilla/5.0'} #Needed to prevent 403 error on Wikipedia
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page, "html.parser")
table = soup.find("table" , { "border" : "1" })
rows=[]
headers = [header.text for header in table.find_all('th')]
for row in table.find_all('tr'):
rows.append([val.text.encode('utf8') for val in row.find_all('td')])
with open('/home/hirdesh/cronrun/iop_status.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(headers)
writer.writerows(row for row in rows if row)
#to insert into db
# Establish a MySQL connection
database = MySQLdb.connect (host="localhost", user = "hfhgfh", passwd = "fghgfhfgh", db = "rghfghj")
cursor = database.cursor()
csv_data = csv.reader(file('/home/hirdesh/cronrun/iop_status.csv'))
query1='''truncate table iop_status'''
cursor.execute(query1)
file = open("/home/hirdesh/cronrun/iop_stauslog.txt", "wb")
#file.write("1.Old data deleted From Master Less table\n")
rows=len(list(csv.reader(open('/home/hirdesh/cronrun/iop_status.csv'))))
i=1
j=rows
query2='''INSERT INTO iop_status (CIRCLE, SSA, Switch, CES_NAME, CES_IP, IOP_A_STATUS, IOP_B_STATUS, IOP_TESTING_DATE, IOP_STATUS,CURNT_DATE) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'''
#query2='''INSERT INTO iop_status (CURRENT_DATE) VALUES (%s)'''
for row in itertools.islice(csv_data,i,j):
CIRCLE = row[1]
SSA= row[2]
Switch= row[3]
CES_NAME= row[4]
CES_IP = row[5]
IOP_A_STATUS = row[6]
IOP_B_STATUS = row[7]
IOP_TESTING_DATE= row[8]
IOP_STATUS = row[9]
CURNT_DATE= row[10]
values = (CIRCLE, SSA, Switch,CES_NAME, CES_IP, IOP_A_STATUS, IOP_B_STATUS, IOP_TESTING_DATE, IOP_STATUS,CURNT_DATE)
cursor.execute(query2,values)
cursor.close()
database.commit()
database.close()
# Print results
file.write("2.%d Rows Inserted\n" % j)
file.write("Current date & time " + time.strftime("%c"))
file.write("*****************\n")
file.close()
下面是我想用漂亮的汤阅读的部分表格。在 Ubuntu 14.04 上它运行良好,但在 Ubuntu 17.04 上它只读取第一行和标题。
<pre>
<table border=1 width=70 font size=2 >
<tr><th>S.No.</th><th>Circle</th><th>SSA</th><th>Switch</th><th>CES_NAME</th><th>CES_IP</th><th>IOP_A_STATUS</th><th>IOP_B_STATUS</th><th>IOP_TESTING_DATE</th><th>IOP_STATUS</th><th>CURRENT_DATE</th></tr>
<tr><td height=5px>
1
</td><td height=5px>
RJC
</td><td height=5px>
CTT
</td><td height=5px>
<a href="cdot_ces_status_switch.jsp?userName=CTTCDOT_PARTA">CTTCDOT_PARTA</a>
</td><td height=5px>
RJ-CTG-PTG-CES
</td><td height=5px>
10.84.4.30
</td><td height=5px>
</th></tr>
</body>
</pre>
【问题讨论】:
-
下次使用按钮
{}格式化代码和HTML。 -
创建最小的工作示例。如果您保存在 csv 中,则删除使用数据库的代码。
-
您也可以将 HTML 放入变量
page以在没有请求的情况下运行它。这样我们就可以运行它并测试它。顺便提一句。首先,您可以使用print()检查 requests 是否为您提供了所有预期的数据。 -
您是否尝试打印您的
table变量? -
您在两个系统上使用相同的 Python 版本吗?新的 Ubuntu 可以使用 Python 3 而不是 Python 2 。在控制台/终端中运行它时不会收到任何错误消息吗?
标签: python html csv beautifulsoup html-parsing