在 Python 上使用 BeautifulSoup,我正在嘗試抓取此頁面的子頁面https://www.mmorpg-stat.eu/0_fiche_alliance.php?pays=5&ftr=500208.all&univers=_146更準確地說,標題為“其他信息”的子頁面,其按鈕具有此代碼onclick="fcache12('faCacher');fcache13('ffond_gris'); document.form1_2date.statview.value='2'; document.forms['form1_2date'].submit();return false;"它調用方法fcache12(),fcache13()然后找到表單form1_2date并使用值提交value='2'(值 0 和 1 用于前兩個按鈕)<form name="form1_2date" method="post">問題是,通過單擊按鈕,url 不會更改,因此無法使用 訪問頁面requests.get(),而是requests.post()應該使用該頁面。import requests, urllib.requestfrom bs4 import BeautifulSoupurl = 'https://www.mmorpg-stat.eu/0_fiche_alliance.php?pays=5&ftr=500208.all&univers=_146'input = {} # what to put here?response = requests.post(url, data = input)我讀到要模擬單擊按鈕,我必須確定應該傳入的表單值,并且這些值由<input>表單中的所有標簽確定。<input>里面有12sform1_2date1 <input name="keyf" id="keyf" type="hidden">2 <input type="checkbox" name="zoomgraph_box" id="zoomgraph_box">3 <input type="checkbox" name="zoomgraph_box" id="zoomgraph_box">4 <input name="Submit" type="button" onclick="fcache12('faCacher');fcache13('ffond_gris');document.getElementById('menu_live_a').style.display='none';document.form1_info3.zoomgraph.value=document.getElementsByName('zoomgraph_box')[0].checked;document.form1_info3.choixflash.value=document.getElementsByName('zoomgraph_box')[1].checked;document.forms['form1_info3'].submit();return false;" style="font:9pt Arial, Helvetica, sans-serif;border: thin solid #666666; cursor:pointer; background:#EA6C11; color: #ffffff;-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 6px 6px 6px 6px; " value="Apply">5 <input name="Submit2" type="button" onclick="document.getElementById('menu_live_a').style.display='none';" style="font:9pt Arial, Helvetica, sans-serif;border: thin solid #666666; cursor:pointer;background:#EA6C11; color: #ffffff;-moz-border-radius: 10px;-webkit-border-radius: 10px;border-radius: 6px 6px 6px 6px; " value="Cancel">我讀到字典必須以表格形式編寫,{'key1': value1, 'key2': value2, ...}但我不明白如何編譯它。
1 回答

蕭十郎
TA貢獻1815條經驗 獲得超13個贊
在您的網絡瀏覽器中,打開開發工具(您可以通過按 來打開F12
)
然后打開network
選項卡,您將看到瀏覽器發出的每個請求。
現在,單擊Other information
網頁上的按鈕。那么,在開發工具中你會看到一些請求,你需要的是這個:
當您選擇它時,它會在右側面板上打開請求的詳細信息。
因此,要使您的請求生效,您必須模仿這一點;屏幕截圖上顯示了Params選項卡,這可能是最重要的,但您還需要獲取 cookie(您可以使用requests.SessionPHPSESSID)
這有效:
import requests
response = requests.post(
'https://www.mmorpg-stat.eu/0_fiche_alliance.php?pays=5&ftr=500208.all&univers=_146',
data = {
'date_1': '01-10-2019',
'date_2': '19-10-2019',
'statview': 2,
}
)
print(response.text)
添加回答
舉報
0/150
提交
取消