防止“隐藏"在 Python 中使用 urlopen() 重定向 [英] Preventing a "hidden" redirect with urlopen() in Python

查看:26
本文介绍了防止“隐藏"在 Python 中使用 urlopen() 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 BeautifulSoup 进行网页抓取,并且在使用 urlopen 时遇到特定类型网站的问题.网站上的每个项目都有自己独特的页面,并且项目有不同的格式(例如:500 mL、1L、2L...).

I am using BeautifulSoup for web scraping and I am having problems with a particular type of website when using urlopen. Every item on the website has its own unique page and the item comes in different formats (ex: 500 mL, 1L, 2L,...).

当我使用 Internet 浏览器打开产品的 URL (www.example.com/product1) 时,我会看到一张 500 mL 格式的图片,以及有关它的信息 (价格、数量、风味等)以及可用于此特定项目的所有其他格式的列表.如果单击另一种格式(例如:1L),图片和有关该项目的信息会发生变化,但浏览器顶部的 URL 将保持不变(www.example.com/product1).但是,通过检查页面的 HTML 代码,我知道所有格式都有自己唯一的 URL (500 mL : www.example.com/product1/123; 1L : www.example.com/product1/456,...).在我的浏览器中使用1L格式的唯一URL时,我自动重定向到页面www.example.com/product1但页面上显示的图片和信息对应于1L格式.HTML 代码还包含我需要的有关 1L 格式的信息.

When I open the URL of the product (www.example.com/product1) using my Internet Browser, I would see a picture of the 500 mL format, information about it (price, quantity, flavor, etc.) and a list of all the other formats available for this specific item. If a click on another format (ex: 1L), the picture and the information about the item would change but the URL at the top of my browser would stay the same (www.example.com/product1). However, I know by inspecting the HTML code of the page that all the format have their own unique URL (500 mL : www.example.com/product1/123; 1L : www.example.com/product1/456, ...). When using the unique URL of the 1L format in my Internet Browser, I am automatically redirected to the page www.example.com/product1 but the picture and the information displayed on the page corresponds to the 1L format. The HTML code also contains the information that I need about the 1L format.

当我使用 urlopen 打开这些唯一的 URL 时,我的问题出现了.

My problem arises when I use urlopen to open these unique URLs.

from bs4 import BeautifulSoup 
from urllib import urlopen
webpage = urlopen('www.example.com/product1/456')
soup=BeautifulSoup(webpage)
print soup    

汤中包含的信息对应于使用我的 Internet 浏览器显示的唯一 URL:www.example.com/product1/456.它为我提供了有关 www.example.com/product1 上默认显示的项目格式的信息,该格式始终为 500 毫升格式.

The information contained in the soup does not correspond to the information displayed using my Internet Browser for the unique URL: www.example.com/product1/456. It gives me the information about the item format displayed by default on www.example.com/product1 which is always the 500 mL format.

有什么方法可以阻止这种重定向,让我可以使用 BeautifulSoup 捕获唯一 URL 的 HTML 代码中包含的信息?

Is there any way I can prevent this redirection that would allow me to capture with BeautifulSoup the information contained in the HTML code of the unique URLs?

推荐答案

import urllib2

class RedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp)
        result.status = code
        return result
    http_error_301 = http_error_303 = http_error_307 = http_error_302

opener = urllib2.build_opener(RedirectHandler())
webpage = opener.open('http://www.example.com/product1/456')
...

这篇关于防止“隐藏"在 Python 中使用 urlopen() 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆