如何使用BeautifulSoup从带有保护的重定向网站获取html内容?

2024-10-01 07:51:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试从网页获取html内容时遇到问题。在

在这个网站:https://tmofans.com/library/manga/5763/nisekoi当你在“capitolo 230.00”中点击播放图标时,它打开下一个链接:https://tmofans.com/goto/347231会将你重定向到这个网站:https://tmofans.com/viewer/5c187dcea0240/paginated

问题是当你直接打开这个链接:https://tmofans.com/goto/347231页面给出了403禁止的消息。 重定向到最终页面的唯一方法是从第一页单击“播放”按钮。在

我想只使用tmofas.com/goto网站链接

我尝试使用请求和BeautifulSoup来获取html内容

import requests
from BeautifulSoup import BeautifulSoup

response = requests.get("https://tmofans.com/goto/347231") 
page = str(BeautifulSoup(response.content))

print page

当我用https://tmofans.com/goto/347231执行此操作时,我只得到403禁止页的内容。在


Tags: httpsimportcom内容网站链接responsehtml
2条回答

这个网站检查你是否有他们网站的推荐人,否则会给你403的回复。通过设置一个referer,您可以轻松地绕过这个问题。在

import requests
ref='https://tmofans.com'
headers = { 'Referer': ref }
r = requests.get('https://tmofans.com/goto/347231',headers=headers)
print(r.url)
print(r.status_code)

输出

^{pr2}$

有一次我用http.client和我的浏览器设法删除了一些受保护的页面。在

我首先导航到需要访问的页面,然后使用浏览器的开发工具复制了请求标题并将其用于脚本中。这样,您的脚本将以浏览器访问资源的方式访问资源。在

这两种方法可以帮助您,首先解析HTTP请求以获取头文件(request和body可能也有帮助,具体取决于您的情况),然后使用第二种方法下载文件。在

这可能需要你做些调整才能奏效。在

from http.client import HTTPSConnection

def parse_headers(http_post):
    """Converts a header string to a dictionnary of its attributes."""

    # Regex to extract headers
    req_line = re.compile(r'(?P<method>GET|POST)\s+(?P<resource>.+?)\s+(?P<version>HTTP/1.1)')
    field_line = re.compile(r'\s*(?P<key>.+\S)\s*:\s+(?P<value>.+\S)\s*')

    first_line_end = http_post.find('\n')
    headers_end = http_post.find('\n\n')
    request = req_line.match(http_post[:first_line_end]).groupdict()
    headers = dict(field_line.findall(http_post[first_line_end:headers_end]))
    body = http_post[headers_end + 2:]

    return request, headers, body


def get_file(url, domain, headers, temp_directory):
    """
    Fetches the file located at the provided URL and returns the content.
    Uses `headers` to bypass auth.
    """
    conn = HTTPSConnection(domain)
    conn.request('GET', url, headers=headers)
    response = conn.getresponse()
    content_type = response.getheader('Content-Type')
    content_disp = response.getheader('Content-Disposition')

    # Change to whatever content type you need
    if content_type != 'application/pdf':
        conn.close()
        return
    else:
        file_content = response.read()
        conn.close()
        return file_content

标题字符串应如下所示:

^{pr2}$

它可能会根据网站的不同而改变,但使用这些可以让我在登录后下载文件。在

相关问题 更多 >