我想iframe从网页上获取所有内容.
码:
site = "http://" + url
f = urllib2.urlopen(site)
web_content = f.read()
soup = BeautifulSoup(web_content)
info = {}
content = []
for iframe in soup.find_all('iframe'):
info['src'] = iframe.get('src')
info['height'] = iframe.get('height')
info['width'] = iframe.get('width')
content.append(info)
print(info)
pprint(content)
Run Code Online (Sandbox Code Playgroud)
结果print(info):
{'src': u'abc.com', 'width': u'0', 'height': u'0'}
{'src': u'xyz.com', 'width': u'0', 'height': u'0'}
{'src': u'http://www.detik.com', 'width': u'1000', 'height': u'600'}
Run Code Online (Sandbox Code Playgroud)
结果pprint(content):
[{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', …Run Code Online (Sandbox Code Playgroud) 我是 Python 新手,对于我的一个项目,我需要将 csv 转换为嵌套 Json。在网上搜索,我发现pandas对这种情况很有帮助。我按照将CSV 数据转换为 Python 中的嵌套 JSON 中给出的方法进行操作
,但我收到了 keyError 异常KeyError: 'state'
df info
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
country 4 non-null object
state 4 non-null object
city 4 non-null object
dtypes: object(3)
memory usage: 176.0+ bytes
None
Traceback (most recent call last):
File "csvToJson.py", line 31, in <module>
grouped = df.groupby(['country', 'state'])
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/pandas/core/generic.py", line 7632, in groupby
observed=observed, **kwargs)
File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line …Run Code Online (Sandbox Code Playgroud)