【发布时间】:2023-04-01 07:03:01
【问题描述】:
我创建了一个快速的 Python 程序,它返回 URL 最终目的地的标题。
def get_title(url):
try:
req = urllib2.Request(url)
soup = BeautifulSoup(urllib2.urlopen(req))
return soup.title.string.encode('ascii', 'ignore').strip().replace('\n','')
except:
print('Generic Exception for ' + url + ', ' + traceback.format_exc())
此代码工作正常,但其中一个 URL 具有通过 window.location 完成的重定向,因此我的脚本无法遵循该路径。有没有一种简单的方法让它也跟随window.location 重定向?
【问题讨论】:
-
遍历脚本,使用适当的正则表达式找到文本
window.location = "...",转到匹配的字符串。
标签: python beautifulsoup urllib2