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']
您可以在此处看到更多信息: