【发布时间】:2020-07-10 18:09:50
【问题描述】:
我正在构建一个脚本来扫描网站并捕获 URL 并测试它是否正常工作。问题是该脚本只查找网站主页的 URL,而将其他 URL 放在一边。如何捕获链接到该网站的所有页面?
在我的代码附件下面:
import urllib
from bs4 import BeautifulSoup
import re
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
page = urllib.request.urlopen("http://www.google.com/")
soup = BeautifulSoup(page.read(), features='lxml')
links = soup.findAll("a", attrs={'href': re.compile('^(http://)')})
for link in links:
result = (link["href"])
req = Request(result)
try:
response = urlopen(req)
pass
except HTTPError as e:
if e.code != 200:
# Stop, Error!
with open("Document_ERROR.txt", 'a') as archive:
archive.write(result)
archive.write('\n')
archive.write('{} \n'.format(e.reason))
archive.write('{}'.format(e.code))
archive.close()
else:
# Enjoy!
with open("Document_OK.txt", 'a') as archive:
archive.write(result)
archive.write('\n')
archive.close()
【问题讨论】:
-
不探索外部和内部什么意思?
-
链接到页面的那些链接。内部是网站
-
我还是不明白,你想做什么?
-
建议是在网站上查找损坏的链接。在搜索中,它只找到链接到主页的 URL,而不扫描其他站点。
-
我在代码中看不到任何可以做到这一点的东西。不过,我可能会遗漏一些东西。
标签: python html web beautifulsoup