我的代码是搜索在命令提示符中传递的链接,获取链接处网页的HTML代码,搜索网页上链接的HTML代码,然后对找到的链接重复这些步骤。我希望这是清楚的。你知道吗
它应该打印出任何导致错误的链接。你知道吗
更多需要的信息:
它最多可以访问100次。 如果网站有错误,则返回None值。你知道吗
我用的是Python3
例如:
s = readwebpage(url)... # This line of code gets the HTML code for the link(url) passed in its argument.... if the link has an error, s = None.
该网站的HTML代码在其网页上有以p2.html
、p3.html
、p4.html
和p5.html
结尾的链接。我的代码读取所有这些,但它不会单独访问这些链接来搜索更多的链接。如果这样做了,它应该搜索这些链接并找到一个以p10.html结尾的链接,然后它应该报告以p10.html结尾的链接有错误。很明显,现在还没有,这让我很难受。你知道吗
我的密码。。你知道吗
url = args.url[0]
url_list = [url]
checkedURLs = []
AmountVisited = 0
while (url_list and AmountVisited<maxhits):
url = url_list.pop()
s = readwebpage(url)
print("testing url: http",url) #Print the url being tested, this code is here only for testing..
AmountVisited = AmountVisited + 1
if s == None:
print("* bad reference to http", url)
else:
urls_list = re.findall(r'href="http([\s:]?[^\'" >]+)', s) #Creates a list of all links in HTML code starting with...
while urls_list: #... http or https
insert = urls_list.pop()
while(insert in checkedURLs and urls_list):
insert = urls_list.pop()
url_list.append(insert)
checkedURLs = insert
请帮忙:)
不是Python,但是因为您提到您没有严格地绑定到
regex
,所以我认为您可能会发现使用wget
来实现这一点有些用处。你知道吗细分:
spider
:使用此选项调用Wget时,Wget将表现为一个webspider,这意味着它不会下载页面,只需检查页面是否存在。-o C:\wget.log
:将所有消息记录到C:\日志.-e robots=off
:忽略机器人.txt-w 1
:设置1秒的等待时间-r
:设置递归搜索-l 10
:将递归深度设置为10,这意味着wget的深度只能达到10级,这可能需要根据最大请求数进行更改http://www.stackoverflow.com
:要以其开头的URL完成后,您可以查看
wget.log
条目,通过搜索HTTP状态码404
等来确定哪些链接有错误这是你想要的密码。但是,请停止使用regex解析HTML。漂亮的组合才是最好的选择。你知道吗
http
,并且[\s:]
匹配“某种空格(即\s
)或:”我将正则表达式改为:
urls_list = re.findall(r'href="(.*)"',s)
。也称为“在href=”之后匹配引号中的任何内容。如果您确实需要确保http[s]://,请使用r'href="(https?://.*)"'
(s?
=>;一或零s
)编辑:使用实际工作的正则表达式,使用非greedglom:
href=(?P<q>[\'"])(https?://.*?)(?P=q)'
(另外,呃,虽然在你的例子中这在技术上是不必要的,因为} 的习惯是一种很好的做法。)
re
缓存,但我认为养成使用^{相关问题 更多 >
编程相关推荐