我试图通过点击 2 个单选按钮从该网站 http://rgphentableaux.hcp.ma/Default1/ 中抓取数据,然后从这样的列表中进行选择 enter image description here
我需要为该列表中的所有可用选项执行此操作,并将表添加到我拥有的数据框中 这是我到目前为止尝试过的,但没有用
from bs4 import BeautifulSoup
pip install selenium
from selenium import webdriver
browser=webdriver.Chrome()
url = "http://rgphentableaux.hcp.ma/Default1/"
browser.get(url) #navigate to the page
browser.find_element_by_xpath(".//input[@type='radio' and
@value='5']").click()
browser.find_element_by_id("CGEO").click()
time.sleep(3)
browser.find_element_by_xpath(".//input[@type='button' and
@value='Afficher']").click()
tabs = browser.find_elements_by_id('IEE')
innerHTML = browser.execute_script("return
document.body.innerHTML")
soup_level2=BeautifulSoup(innerHTML, 'html.parser')
答案 0 :(得分:1)
通过模仿页面发出的请求,您可以使用 <div class="row row-cols-4 row-cols-md-8 row-cols-lg-12">
<div class="col-6"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
<div class="col"><div style="width:100%; background: #ccc; height: 2.4rem;"></div></div>
</div>
</div>
和 requests
完成整个操作。您只需按正确的顺序循环区域,并将当前区域编号添加到每个请求的 bs4
参数中。
这个:
'CGEO'
从登陆网址收集区域名称的初始列表。
这个:
soup = bs(s.get(url).content, 'lxml')
regions = [i.text.strip() for i in soup.select('#REGIONSLIST option')]
使用 for k,v in regions.items():
params = (('type', 'Region'), ('CGEO', v), ('them', '5'))
标签 CGEO
属性为区域设置 option
参数,例如
value
是 Tanger-Tetouan-Al Hoceima
。
'01'
选项设置在 Region
参数中。
type
选项设置在 Langues locales utilisées
参数中,即 them
。
这个:
'5'
只是颠倒项目的顺序,使得 for y in range(3):
row.extend([data[i-y+2]['DATA2014']])
内每个字典中的 Ens, Fem, Masc
按照所需的 data
输出顺序添加到 row
。
Py:
Masc, Fem, Ens
编辑:
要获得所有 3 个表(ensemble、urbain、rural),请按如下所示调整自定义函数并添加附加循环 import requests
import pandas as pd
from bs4 import BeautifulSoup as bs
def add_rows(region, data):
for i in range(0, len(data)//3, 3):
row = [region, data[i]['INDICATEUR'].split('_')[-1]]
for y in range(3):
row.extend([data[i-y+2]['DATA2014']])
final.append(row)
url = 'http://rgphentableaux.hcp.ma/Default1'
headers= {'User-Agent': 'Mozilla/5.0', 'Referer': url}
final = []
with requests.Session() as s:
s.headers = headers
soup = bs(s.get(url).content, 'lxml')
regions = {i.text.strip():i['value'].strip() for i in soup.select('#REGIONSLIST option')}
for k,v in regions.items():
params = (('type', 'Region'), ('CGEO', v), ('them', '5'))
r = s.get(f'{url}/getDATA/', params=params)
data = r.json()
add_rows(k, data)
df = pd.DataFrame(final, columns = ['Region', 'Lang', 'Masc', 'Fem', 'Ens'])
print(df)
:
for n in range(0, len(data), block)
答案 1 :(得分:0)
要选择文本为 Langues locales utilisées 和 Region 的项目并抓取表格,您可以使用以下解决方案:
driver.get("http://rgphentableaux.hcp.ma/Default1/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@text='Langues locales utilisées']"))).click()
driver.find_element_by_xpath("//input[@value='Region']").click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Choisir une entitée']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='Choisir une entitée']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[contains(., 'Tanger-Tetouan-Al Hoceima')]"))).click()
driver.find_element_by_xpath("//input[@value='Afficher']").click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table[@class='tableau']/tbody"))).text)
控制台输出:
Population municipale 16 747 522 16 862 562 33 610 084
Répartition selon les grands groupes d'âges
Moins de 6 ans 12.4 11.8 12.1
De 6 à 14 ans 16.5 15.7 16.1
De 15 à 59 ans 61.8 63.0 62.4
60 ans et plus 9.3 9.5 9.4
Répartition selon le groupe d'âges quinquennal
0-4 ans 10.4 9.9 10.2
5-9 ans 9.2 8.8 9.0
10-14 ans 9.3 8.8 9.0
15-19 ans 8.9 8.8 8.9
20-24 ans 9.0 9.1 9.1
25-29 ans 8.2 8.4 8.3
30-34 ans 7.7 8.0 7.8
35-39 ans 6.8 7.2 7.0
40-44 ans 6.3 6.5 6.4
45-49 ans 5.3 5.6 5.4
50-54 ans 5.3 5.4 5.3
55-59 ans 4.2 4.0 4.1
60-64 ans 3.4 3.3 3.4
65-69 ans 1.9 1.9 1.9
70-74 ans 1.6 1.8 1.7
75 ans et plus 2.4 2.6 2.5
État matrimonial
Célibataire 57.9 48.4 53.2
Marié 40.8 42.0 41.4
Divorcé 0.7 2.4 1.6
Veuf 0.6 7.1 3.9
Âge moyen au premier mariage 31.3 25.7 28.5
Fécondité
Parité moyenne à 45-49 ans / 3.5 /
Indice synthétique de fécondité / 2.2 /