Python Request Post在韩国网站中没有正确响应



我正试图从韩国网站上抓取一些商品数据。

该网站显示了货船的到达日期、部门日期、母船名称等一般数据。

网站链接

右边的黑色按钮是搜索按钮。

为了从中获取数据,必须设置一些单选按钮,然后点击搜索。

所以我想我可以向网站发出Post请求,这样我就可以从回复中提取数据。

不幸的是,回复只是一个没有Post请求的普通页面。

这是请求后

POST /Berth_status_text_servlet_sw_kr HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Referer: http://info.bptc.co.kr:9084/content/sw/frame/berth_status_text_frame_sw_kr.jsp
Accept-Language: en-US,en;q=0.7,ko;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Content-Length: 40
Host: info.bptc.co.kr:9084
Pragma: no-cache
Connection: close
v_time=month&ROCD=ALL&ORDER=item2&v_gu=S

这就是我在Python 中所做的

from bs4 import BeautifulSoup
import requests
params ={'v_time': 'month',
'ROCD': 'ALL',
'ORDER': 'item2',
'v_gu': 'S'}

response = requests.post(url, data = params)
soup = BeautifulSoup(response.content,"html")
print(soup)

我确实试着把编码和其他东西放在标题中,比如下面的

response = requests.post(url, data = params, 
headers={'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko', 
'Content-type': 'application/x-www-form-urlencoded; text/html; charset=euc-kr',
'Accept-Language': 'en-US,en;q=0.7,ko;q=0.3'
})

它也不起作用。

代码在其他网站上运行良好,所以我想这是与韩国特色有关的东西。

我试着寻找这个问题的解决方案,但运气不好。

你介意帮帮我吗?

谢谢!

您的方法是正确的。响应返回html,您需要将其解析为更可用的格式。如果dicts:,下面的代码将html响应中的表转换为列表

from bs4 import BeautifulSoup
import requests
params = {"v_time": "month", "ROCD": "ALL", "ORDER": "item2", "v_gu": "S"}

response = requests.post(
"http://info.bptc.co.kr:9084/Berth_status_text_servlet_sw_kr", data=params
)
soup = BeautifulSoup(response.content, features="html.parser")
keys = [th.get_text(strip=True) for th in soup("th")]
data = [
{key: value.get_text(strip=True) for key, value in zip(keys, row("td"))}
for row in soup("tr")
]
print(data)

打印:

[
{
"S/H": "0",
"모선항차": "DPYT-21",
"반입 마감일시": "",
"선박명": "PEGASUS YOTTA",
"선사": "DYS",
"선석": "2",
"선적": "0",
"양하": "0",
"입항 예정일시": "2020/06/08 21:00",
"입항일시": "",
"전배": "",
"접안": "P",
"출항 예정일시": "2020/06/09 11:00",
"출항일시": "",
"항로": "NCK",
}
...
]

最新更新