Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 靓汤:查找当前网站的地址_Python_Beautifulsoup - Fatal编程技术网

Python 靓汤:查找当前网站的地址

Python 靓汤:查找当前网站的地址,python,beautifulsoup,Python,Beautifulsoup,我正在编写一个代码,加载很多网站,有时链接不存在,而是转到另一个链接(而不是我告诉它的链接) 因此,我希望能够识别我正在废弃的当前站点何时不是我告诉它要去的地址 这是我正在使用的代码示例。我应该添加什么才能找到它要去的地址的名称 req = Request(l, headers={'User-Agent': 'Mozilla/5.0'}) html_page = urlopen(req).read() soup = BeautifulSoup(html_page,

我正在编写一个代码,加载很多网站,有时链接不存在,而是转到另一个链接(而不是我告诉它的链接)

因此,我希望能够识别我正在废弃的当前站点何时不是我告诉它要去的地址

这是我正在使用的代码示例。我应该添加什么才能找到它要去的地址的名称

req = Request(l, headers={'User-Agent': 'Mozilla/5.0'})
        html_page = urlopen(req).read()
        soup = BeautifulSoup(html_page, "lxml")

有两种方法,可以设置allow_redirects=False以防止请求重定向到另一个页面,也可以检查规范url:

from bs4 import BeautifulSoup
import requests
import urllib
l = 'http://en.wikipedia.org/wiki/Google_Inc_Class_A'
req = requests.get(l, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(req._content, "lxml")
canonical = soup.find('link', {'rel': 'canonical'})
canonical['href']

您可以在此处看到更多信息:

有两种方法,可以设置allow_redirects=False以防止请求重定向到另一个页面,也可以检查规范url:

from bs4 import BeautifulSoup
import requests
import urllib
l = 'http://en.wikipedia.org/wiki/Google_Inc_Class_A'
req = requests.get(l, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(req._content, "lxml")
canonical = soup.find('link', {'rel': 'canonical'})
canonical['href']
您可以在此处看到更多信息: