Line 如果当前URL返回404,如何让python尝试文件中的下一个URL?
我在弄清楚需要创建什么代码才能让python尝试csv文件中的下一个url时遇到了一个问题。每个url都位于这样一行:Line 如果当前URL返回404,如何让python尝试文件中的下一个URL?,line,beautifulsoup,mechanize,next,Line,Beautifulsoup,Mechanize,Next,我在弄清楚需要创建什么代码才能让python尝试csv文件中的下一个url时遇到了一个问题。每个url都位于这样一行: http://www.indexedamerica.com/states/PR/Aguada/Restaurants-Aguada-00602.html http://www.indexedamerica.com/states/PR/Aguadilla/Restaurants-Aguadilla-00604.html http://www.indexedamerica.co
http://www.indexedamerica.com/states/PR/Aguada/Restaurants-Aguada-00602.html http://www.indexedamerica.com/states/PR/Aguadilla/Restaurants-Aguadilla-00604.html http://www.indexedamerica.com/states/PR/Maricao/Restaurants-Maricao-00606.html
将要搜索的所有URL添加到列表中。然后循环浏览列表,按顺序打开每个url。如果给定的url返回任何类型的错误,那么您可以选择使用“继续”忽略该url文件并转到下一个文件。将要搜索的所有url添加到列表中。然后循环浏览列表,按顺序打开每个url。如果给定的url返回任何类型的错误,那么您可以选择使用“继续”忽略该url文件并转到下一个url文件
for line in html:
try:
mechanize.open(html)
table = soup.find("table", border=3)
except Exception:
continue
或者,您可以检查页面的状态代码,如果收到404(在for循环中),则跳过:
continue
在循环中,停止进一步代码的执行并继续循环中的下一个条目
或者,您可以检查页面的状态代码,如果收到404(在for循环中),则跳过:
continue
在循环中,停止执行进一步的代码并继续执行循环中的下一个条目。大约有40k个URL。我应该使用什么类型的列表?字典?元组?只需使用一个列表,并遍历该列表。大约有40k个URL。我应该使用什么类型的列表?字典?tuple?只需使用一个列表,并遍历该列表。
for line in html:
try:
mechanize.open(html)
table = soup.find("table", border=3)
except Exception:
continue
if urllib.urlopen(url).getcode() == '404':
continue