如何使用请求访问此类站点? [英] How can I access this type of site using requests?
本文介绍了如何使用请求访问此类站点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次遇到不允许允许我访问"网页的网站.我不知道为什么,我不知道如何从这个网站上抓取.
This is the first time I've encountered a site where it wouldn't 'allow me access' to the webpage. I'm not sure why and I can't figure out how to scrape from this website.
我的尝试:
import requests
from bs4 import BeautifulSoup
def html(url):
return BeautifulSoup(requests.get(url).content, "lxml")
url = "https://www.g2a.com/"
soup = html(url)
print(soup.prettify())
输出:
<html>
<head>
<title>
Access Denied
</title>
</head>
<body>
<h1>
Access Denied
</h1>
You don't have permission to access "http://www.g2a.com/" on this server.
<p>
Reference #18.4d24db17.1592006766.55d2bc1
</p>
</body>
</html>
我已经研究了一段时间,我发现应该有某种类型的令牌
[访问、刷新等...].
I've looked into it for awhile now and I found that there is supposed to be some type of token
[access, refresh, etc...].
另外,action="/search"
但我不知道该怎么做.
Also, action="/search"
but I wasn't sure what to do with just that.
推荐答案
该页面需要指定一些HTTP头来获取信息(Accept-Language
):
This page needs to specify some HTTP headers to obtain the information (Accept-Language
):
import requests
from bs4 import BeautifulSoup
headers = {'Accept-Language': 'en-US,en;q=0.5'}
def html(url):
return BeautifulSoup(requests.get(url, headers=headers).content, "lxml")
url = "https://www.g2a.com/"
soup = html(url)
print(soup.prettify())
打印:
<!DOCTYPE html>
<html lang="en-us">
<head>
<link href="polyfill.g2a.com" rel="dns-prefetch"/>
<link href="images.g2a.com" rel="dns-prefetch"/>
<link href="id.g2a.com" rel="dns-prefetch"/>
<link href="plus.g2a.com" rel="dns-prefetch"/>
... and so on.
这篇关于如何使用请求访问此类站点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文