我一直在寻找一个HTML表从一个石油生产SSRS提要在线。我已经学会了一些漂亮的汤/巨蟒来达到我目前的水平,但我想我需要一些帮助才能完成它。
其目的是刮除所有标记的表,并输出json数据。我有一个json格式的输出,但是对于10个头,但是它在每个头重复相同的数据行单元格值。我认为通过单元格来分配给标头的迭代是问题所在。我相信跑步的时候会有意义的。
任何帮助都将是非常感谢的,试图了解我做错了什么,因为这对我来说是非常新的。
干杯
import json
from bs4 import BeautifulSoup
import urllib.request
import boto3
import botocore
#Url to scrape
url='http://factpages.npd.no/ReportServer?/FactPages/TableView/
field_production_monthly&rs:Command=Render&rc:Toolbar=
false&rc:Parameters=f&Top100=True&IpAddress=108.171.128.174&
CultureCode=en'
#Agent detail to prevent scraping bot detection
user_agent = 'Mozilla/5(Macintosh; Intel Mac OS X 10_9_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47
Safari/537.36'
header = {'User-Agent': user_agent}
#Request url from list above, assign headers from criteria above
req = urllib.request.Request(url, headers = header)
#Open url from the previous request and assign
npddata = urllib.request.urlopen(req, timeout = 20)
#Start soup on url request data
soup = BeautifulSoup(npddata, 'html.parser')
# Scrape the html table variable from selected website
table = soup.find('table')
headers = {}
col_headers = soup.findAll('tr')[3].findAll('td')
for i in range(len(col_headers)):
headers[i] = col_headers[i].text.strip()
# print(json.dumps(headers, indent = 4))
cells = {}
rows = soup.findAll('td', {
'class': ['a61cl', 'a65cr', 'a69cr', 'a73cr', 'a77cr', 'a81cr', 'a85cr',
'a89cr', 'a93cr', 'a97cr']})
for row in rows[i]: #remove index!(###ISSUE COULD BE HERE####)
# findall function was original try (replace getText with FindAll to try)
cells = row.getText('div')
# Attempt to fix, can remove and go back to above
#for i in range(len(rows)): #cells[i] = rows[i].text.strip()
#print(cells)# print(json.dumps(cells, indent = 4))
#print(cells)# print(json.dumps(cells, indent = 4))
data = []
item = {}
for index in headers:
item[headers[index]] = cells#[index]
# if no getText on line 47 then.text() here### ISSUE COULD BE HERE####
data.append(item)
#print(data)
print(json.dumps(data, indent = 4))
# print(item)#
print(json.dumps(item, indent = 4))发布于 2017-11-09 15:06:36
您的代码中有一些错误--我修复了这些错误,并稍微修改了您的代码:
这就是你想要的吗
import requests
from bs4 import BeautifulSoup
import json
# Webpage connection
html = "http://factpages.npd.no/ReportServer?/FactPages/TableView/field_production_monthly&rs:Command=Render&rc:Toolbar=false&rc:Parameters=f&Top100=True&IpAddress=108.171.128.174&CultureCode=en"
r=requests.get(html)
c=r.content
soup=BeautifulSoup(c,"html.parser")
rows = soup.findAll('td', {
'class': ['a61cl', 'a65cr', 'a69cr', 'a73cr', 'a77cr', 'a81cr', 'a85cr',
'a89cr', 'a93cr', 'a97cr']})
headers = soup.findAll('td', {
'class': ['a20c','a24c', 'a28c', 'a32c', 'a36c', 'a40c', 'a44c', 'a48c',
'a52c']})
headers_list = [item.getText('div') for item in headers]
rows_list=[item.getText('div') for item in rows]
final=[rows_list[item:item+9] for item in range(0,len(rows_list),9)]
row_header={}
for item in final:
for indices in range(0,9):
if headers_list[indices] not in row_header:
row_header[headers_list[indices]]=[item[indices]]
else:
row_header[headers_list[indices]].append(item[indices])
result=json.dumps(row_header,indent=4)
print(result)输出样本:
{
"Year": [
"2009",
"2009",
"2009",
"2009",
"2009",
"2009",
"2010",
"2010",
"2010",
"2010",
"2010",https://stackoverflow.com/questions/47200845
复制相似问题