【发布时间】:2019-08-26 03:27:00
【问题描述】:
我正在尝试从网页中抓取结果表,并最终将这些结果写入 csv 文件。我已经使用 BeautifulSoup 抓取页面,提取包含我需要的数据的 JSON 字符串并让 Pandas 输出该表,但它似乎只是打印表轮廓,而不包含任何行详细信息。
我的代码如下(这可能表明它对编程非常陌生!):
from bs4 import BeautifulSoup
import urllib3
import json
import pandas as pd
from pandas.io.json import json_normalize
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
http = urllib3.PoolManager()
url = '[url_im_scraping]'
headers = urllib3.util.make_headers(basic_auth='[username/password]')
response = http.request('GET', url, headers=headers)
soup = BeautifulSoup (response.data, 'html.parser')
#This extracts the initial table of data
grid_data = soup.find("script", class_="__allTestPointsOfSelectedSuite")
data = json.loads(grid_data.text)
#This was to remove the column settings part of the table
testtest = grid_data.text.split("testPoints")
#Putting "{" and the initial key back into the string and loading into JSON object
print(pd.read_json("{" + "\"testPoints" + testtest[1]))
当我将我的 JSON 字符串加载到创建网站(如 json2table)的 JSON 表中时,它会正确显示并验证为有效的 JSON 字符串,并输出如下内容:
testpoints
Column1 Column2 Column3 Column4 etc...
totalPointsCount
当我尝试使用 Pandas 将 JSON 字符串输出为表格时,我得到以下信息:
testPoints totalPointsCount
0 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
1 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
2 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
3 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
4 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
5 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
6 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
7 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
8 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
9 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
10 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
11 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
12 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
13 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
14 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
15 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
16 {'assignedTo': 'a5060ed2-6b1c-4da3-add0-0d6d97... 17
我不确定如何在“testPoints”和“totalPoundsCount”这两个键中显示嵌套字段。
我希望如何在此处获取输出的示例(使用 Json2table 生成):
希望有人能指出我出错的正确方向。
编辑:我现在已经更改了最大列宽,并且看到我得到了这样返回的整个字符串:
{'assignedTo': 'a5060ed2', 'automated': 'Not Automated', 'build': None, 'configurationId': 123, 'configurationName': 'Package 1.0', 'lastResultState': 1, 'lastRunBy': '', 'lastRunDuration': 0, 'mostRecentResultOutcome': 2, 'mostRecentRunId': 1234, 'outcome': 'Passed', 'state': 2, 'suiteId': 1234, 'suiteName': Name', 'testCaseId': 12345, 'testPointId': 12345, 'tester': 'Fred Smith', 'workItemProperties': [{'Key': 'System.Id', 'Value': 12345}, {'Key': 'System.Title', 'Value': 'Item Item'}, {'Key': 'System.IterationPath', 'Value': 'Path\Path'}, {'Key': 'System.ChangedDate', 'Value': '/Date(1554200489873)/'}, {'Key': 'System.ChangedBy', 'Value': 'Fred Smith'}, {'Key': 'Microsoft.VSTS.TCM.AutomationStatus', 'Value': 'Not Automated'}]}
,但我仍在努力解决如何从该字符串中获取“assignedTo”等作为列标题,即:
assignedTo Automated Build
123456789 Not Automated None
【问题讨论】:
标签: python json python-3.x pandas