在 Python 中获取请求 (OpenGraph)
Fetch an request in Python (OpenGraph)
我已尝试检查代码并获取API请求,将其导出到 Postman 并在 Python 上获取代码。但是,它不起作用。我继续调查该请求,它不是普通的 http 请求 (API REST),它包含一些应用程序结构 (OpenGraph),我从来没有用它来引导。我正在做几个更改输入参数的测试,但我在那里有点挣扎。
Somecan可以带我吗?谢谢。
试试这个:-
import urllib.request
from bs4 import BeautifulSoup as BS
def getHTML(address):
try:
req = urllib.request.Request(address)
req.add_header(
'User-Agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36')
with urllib.request.urlopen(req) as response:
charset = response.headers.get_content_charset()
if not charset:
charset = 'utf-8' # guess
return response.read().decode(charset)
except Exception as e:
print(e)
# If anygoes wrong, return None
url = 'https://opensea.io/assets/neon-district-season-one-item?search[query]=mimir%20assault&search[sortAscending]=true&search[sortBy]=PRICE&search[stringTraits][0][name]=Armor%20Slot&search[stringTraits][0][values][0]=Body&search[stringTraits][1][name]=rarity&search[stringTraits][1][values][0]=Common'
html = getHTML(url)
if html:
soup = BS(html, 'html.parser')
我已尝试检查代码并获取API请求,将其导出到 Postman 并在 Python 上获取代码。但是,它不起作用。我继续调查该请求,它不是普通的 http 请求 (API REST),它包含一些应用程序结构 (OpenGraph),我从来没有用它来引导。我正在做几个更改输入参数的测试,但我在那里有点挣扎。
Somecan可以带我吗?谢谢。
试试这个:-
import urllib.request
from bs4 import BeautifulSoup as BS
def getHTML(address):
try:
req = urllib.request.Request(address)
req.add_header(
'User-Agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36')
with urllib.request.urlopen(req) as response:
charset = response.headers.get_content_charset()
if not charset:
charset = 'utf-8' # guess
return response.read().decode(charset)
except Exception as e:
print(e)
# If anygoes wrong, return None
url = 'https://opensea.io/assets/neon-district-season-one-item?search[query]=mimir%20assault&search[sortAscending]=true&search[sortBy]=PRICE&search[stringTraits][0][name]=Armor%20Slot&search[stringTraits][0][values][0]=Body&search[stringTraits][1][name]=rarity&search[stringTraits][1][values][0]=Common'
html = getHTML(url)
if html:
soup = BS(html, 'html.parser')