首页 > 解决方案 > 使用 Python 获取公共网站的内容

问题描述

我面临着一个真正的谜团:

VBA

 .send "land_abk=sh&ger_name=Norderstedt&order_by=2&ger_id=X1526"

Python

headers = {'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding':'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive','Referer': 'https://url'}

单击链接会导致最后一个子 ULR 和详细信息。我真的尝试了从 3. 站点获取数据,使用POST、GET、VBA、PYTHON-Referer,但没有成功。我只得到header response 200 & header-content,但没有来自源代码的一个字母,只是一个没有任何描述的错误。没有错误且有内容地打开此第 3 页的唯一方法是单击第 2 页上的链接。这是一个完全公开的网站,没有理由建立referer 或任何其他加密。那么问题是什么以及如何解决呢?

标签: pythonexcelvbawebscreen-scraping

解决方案


只要您包含正确的推荐人,您的标题应该可以正常工作。您接收 html 的方式可能有问题。这对我有用:

使用 urllib3

import urllib3
from bs4 import BeautifulSoup

URL = "https://www.zvg-portal.de/index.php?button=showZvg&zvg_id=755&land_abk=sh"
headers = {
    "Referer": "https://www.zvg-portal.de/index.php?button=Suchen",
}

http = urllib3.PoolManager()
response = http.request("GET", URL, headers=headers)
html = response.data.decode("ISO-8859-1")

soup = BeautifulSoup(html, "lxml")
print(soup.select_one("tr td b").text)
# >> 0061 K 0012/ 2019

使用请求

import requests

URL = "https://www.zvg-portal.de/index.php?button=showZvg&zvg_id=755&land_abk=sh"

headers = {
    "Referer": "https://www.zvg-portal.de/index.php?button=Suchen",
}
html = requests.get(URL, headers=headers).text

print("Versteigerung im Wege der Zwangsvollstreckung" in html)
# >> True

使用 Python 2:

import urllib2

URL = "https://www.zvg-portal.de/index.php?button=showZvg&zvg_id=755&land_abk=sh"

req = urllib2.Request(URL)
req.add_header("Referer", "https://www.zvg-portal.de/index.php?button=Suchen")
html = urllib2.urlopen(req).read()

print("Versteigerung im Wege der Zwangsvollstreckung" in html)
# >> True

推荐阅读