Python 巨蟒靓汤桌刮
我一直在寻找刮从石油生产SSRS饲料在线HTML表。我已经学习了一些漂亮的汤/python来达到我目前的目的,但是我想我需要一些帮助来完成它 其目的是清除所有标记的表并输出json数据。我有一个json格式的输出,但是对于10个头,但是每个头重复相同的数据行单元格值。我认为要分配给标题的单元格的迭代是问题所在。我相信它在运行时会有意义 任何帮助都将不胜感激,尝试了解我做错了什么,因为这对我来说是非常新的 干杯Python 巨蟒靓汤桌刮,python,json,beautifulsoup,Python,Json,Beautifulsoup,我一直在寻找刮从石油生产SSRS饲料在线HTML表。我已经学习了一些漂亮的汤/python来达到我目前的目的,但是我想我需要一些帮助来完成它 其目的是清除所有标记的表并输出json数据。我有一个json格式的输出,但是对于10个头,但是每个头重复相同的数据行单元格值。我认为要分配给标题的单元格的迭代是问题所在。我相信它在运行时会有意义 任何帮助都将不胜感激,尝试了解我做错了什么,因为这对我来说是非常新的 干杯 import json from bs4 import BeautifulSoup i
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))
您的代码中有一些错误,我修复了这些错误并稍微修改了您的代码: 这就是你想要的吗:
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",
缩进在Python中很重要,请确保您的代码示例具有正确的缩进。非常感谢@ayodhyankit Paul,我将在稍后进行研究,看看您修复了什么,从中学习会很好。是否有一种简单的方法可以记录每行?因此,对于每一行,都有一个json记录,其中包含10个标题,那么下一条记录将再次包含第二行已删除数据的标题?@Chris您希望每一行都包含这些标题,可以给我举个例子吗?如果可能,可以这样做,但不确定更改是否简单:[{“field”:alva,“year”:“2010”,“month”:“3”,“Oil”:“3233”,},{“油田”:阿尔瓦,“年份”:“2010年”,“月份”:“3”,“石油”:“4556.”,}]