【发布时间】:2019-03-25 11:37:01
【问题描述】:
我正在尝试使用我命名为 law.html 的模板文件在 Django 中创建一个表,该模板文件中的数据格式化为来自我创建的用于从公共网页中抓取信息的函数的数据帧。我正在尝试使用 for 循环来遍历数据,但由于某种原因无法获得所需的输出。
到目前为止,我有一个名为 newlaw 的 DataFrame,它由函数 all_data 调用。数据框 newlaw 是律师姓名和办公室的列表。然后我将all_data 导入到我的views.py 文件夹中并给它字典all_data。在我的law.html 文件夹中,我正在尝试使用 for 循环创建一个表,以便我可以将每条数据放在一个单元格中。
我的views.py中的代码
def law_view(request, *args, **kwargs):
data = combine_data()
return render(request, "law.html", {'data': data})
The code in my law.html
```<table class="table table-striped">
<thead>
<tr>
<th>Solicitor_Names</th>
<th>Offices</th>
</tr>
</thead>
<tbody>
{%for solicitor in all_data%}
<tr>
<td>{{ solicitor }}</td>
</tr>
{% ednfor %}
</tbody>
</table>```
此代码仅打印列名。我想要的输出看起来像,
Solicitor_Name Office
John Marston Ernst & Young
Amy Smith Kingston Smith
....
....
这是 all_data = combine()
def combine():
from bs4 import BeautifulSoup
import requests
import pandas as pd
urlh = 'http://solicitors.lawsociety.org.uk/search/results?Type=1&IncludeNlsp=True&Pro=True¶meters=%2C1%3BAPL%2C0%3B%2C1%3BPUB%2C0%3B%2C1%3BADV%2C0%3B%2C1%3BAGR%2C0%3B%2C1%3BAVI%2C0%3B%2C1%3BBAN%2C1%3B%2C1%3BBEN%2C0%3B%2C1%3BCHA%2C0%3B%2C1%3BCHI%2C0%3B%2C1%3BCLI%2C0%3B%2C1%3BCOL%2C1%3B%2C1%3BPCO%2C1%3B%2C1%3BCCL%2C0%3B%2C1%3BCOS%2C1%3B%2C1%3BCOM%2C1%3B%2C1%3BCON%2C1%3B%2C1%3BCSU%2C0%3B%2C1%3BCSF%2C0%3B%2C1%3BCSG%2C0%3B%2C1%3BCUT%2C0%3B%2C1%3BCTR%2C1%3B%2C1%3BPRE%2C0%3B%2C1%3BCFI%2C1%3B%2C1%3B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C1%3BPRP%2C0%3B%2C1%3BPRT%2C0%3B%2C1%3BPRW%2C0%3B%2C1%3BPCI%2C0%3B%2C1%3BPCP%2C0%3B%2C1%3BPCT%2C0%3B%2C1%3BPCW%2C0%3B%2C1%3BPNE%2C0%3B%2C1%3BTAX%2C0%3B%2C1%3BTAC%2C1%3B%2C1%3BTAE%2C0%3B%2C1%3BTAH%2C1%3B%2C1%3BTAM%2C0%3B%2C1%3BTAP%2C0%3B%2C1%3BTAT%2C0%3B+'
r = requests.get(urlh)
soup = BeautifulSoup(r.content, 'html.parser')
names = []
roles = []
offices = []
locations = []
for i in range(1,2):
url = 'http://solicitors.lawsociety.org.uk/search/results?Type=1&IncludeNlsp=True&Pro=True¶meters=%2C1%3BAPL%2C0%3B%2C1%3BPUB%2C0%3B%2C1%3BADV%2C0%3B%2C1%3BAGR%2C0%3B%2C1%3BAVI%2C0%3B%2C1%3BBAN%2C1%3B%2C1%3BBEN%2C0%3B%2C1%3BCHA%2C0%3B%2C1%3BCHI%2C0%3B%2C1%3BCLI%2C0%3B%2C1%3BCOL%2C1%3B%2C1%3BPCO%2C1%3B%2C1%3BCCL%2C0%3B%2C1%3BCOS%2C1%3B%2C1%3BCOM%2C1%3B%2C1%3BCON%2C1%3B%2C1%3BCSU%2C0%3B%2C1%3BCSF%2C0%3B%2C1%3BCSG%2C0%3B%2C1%3BCUT%2C0%3B%2C1%3BCTR%2C1%3B%2C1%3BPRE%2C0%3B%2C1%3BCFI%2C1%3B%2C1%3BCRD%2C0%3B%2C1%3BCRF%2C0%3B%2C1%3BCRG%2C0%3B%2C1%3BCRJ%2C0%3B%2C1%3BCRL%2C0%3B%2C1%3BCRM%2C0%3B%2C1%3BCRS%2C0%3B%2C1%3BCRO%2C1%3B%2C1%3BDEB%2C0%3B%2C1%3BDTR%2C1%3B%2C1%3BDEF%2C0%3B%2C1%3BDRC%2C0%3B%2C1%3BDRO%2C1%3B%2C1%3BEDU%2C0%3B%2C1%3BELC%2C0%3B%2C1%3B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+ '=&Page=' + str(i)
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
hp_sol_data = soup.find_all('section', {'class':'solicitor'})
for sol in hp_sol_data:
try:
addy = sol.contents[7].find_all('dd', {'class':'feature highlight'})[0].text
locations.append(addy)
except IndexError:
locations.append('None Found')
try:
office_names = sol.contents[7].find_all('dd', {'class':'highlight'})[0].text
offices.append(office_names.strip())
except IndexError:
offices.append('None Found')
for link in soup.find_all('a', href=True):
if link.get('href').startswith('/person/'):
tags = (link.get('href'))
url2 = 'http://solicitors.lawsociety.org.uk' + str(tags)
r2 = requests.get(url2)
soup = BeautifulSoup(r2.content, 'html.parser')
s_data = soup.find_all('article', {'class':'solicitor solicitor-type-individual details'})
for item in s_data:
solicitor_names = (item.contents[3].find_all('h1')[0].text)
names.append(solicitor_names)
try:
role = (item.find_all('div', {'class':'panel-half'})[1].find('dd').get_text(''))
roles.append(role.strip())
except IndexError:
roles.append('Role not specified')
tls_solicitors = pd.DataFrame({'Solicitor_Name': names, 'Role': roles, 'Office': offices,'Address': locations},
columns = ['Solicitor_Name', 'Office', 'Address', 'Role'])
law = tls_solicitors
newd = law['Role'].str.split('\n', n=3, expand = True)
#law['Primary_Role'] = newd[0]
#law['Secondary_Role'] = newd[1]
role_1 = newd[0]
role_2 = newd[1]
law.drop('Role', axis=1)
all_data = [{'name': names, 'office': offices, 'address': locations, 'primary_role': role_1, 'secondary_role': role_2}]
return all_data
【问题讨论】:
-
你能告诉我们你的
all_data包含什么吗?
标签: python django datatables