在下面的代码中,我试图从imdb站点中提取一些数据。我正在对存储在csv文件中的标题(tt000001,tt000002等)进行迭代,并将迭代后的值放入地址中我正在使用代理来避免出现连接错误,因此我将代码放在try和except块中,以便在出现任何问题时可以更改代理,并且程序可以继续运行而不会被中断。
for i in sheet2.iter_cols(min_row=2,max_row=diff+2,min_col=1,max_col=1):
for j in i:
try:
print("getting address")
req=requests.get("https://www.imdb.com/title/"+str(j.value),proxies=pro,headers=headers)
soup=bs4.BeautifulSoup(req.text,'html.parser')
x=soup.find('div',class_="title_wrapper")
list1.append(x.h1.getText())
print(list1)
except:
print("Proxy {} not working, changing it".format(pro))
pro=oneproxypls()
headers={'User-Agent':ua.random}
else:
print("Written in the {} successfully".format(j.value))
问题是当遇到错误时,它正在更改代理,但如果下一个代理也不起作用,它会跳过该迭代,有时会重复两次或更多次。所以我的问题是,有什么办法因此,在更改代理后,它不会跳过该迭代。谢谢!!
答案 0 :(得分:0)
这需要工作:
private void saveImage() {
if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showLoading("Saving...");
filename = String.valueOf(System.currentTimeMillis()) + ".png";
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "/" + getResources().getString(R.string.app_name));
try {
if (!file.exists())
file.mkdir();
file.createNewFile();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mPhotoEditor.saveAsFile(file.getAbsolutePath() + "/" + filename, new PhotoEditor.OnSaveListener() {
@Override
public void onSuccess(@NonNull String imagePath) {
hideLoading();
showSnackbar("Image Saved Successfully");
MediaScannerConnection.scanFile(EditImageActivity.this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Log.e(".provider", " : " + getPackageName() + ".provider");
Log.e("filepath", " : " + imagePath);
if (imagePath != null && !imagePath.isEmpty()) {
File file1 = new File(imagePath);
Log.e("file1", " : " + file1);
if (file1 != null) {
Uri imageUri = FileProvider.getUriForFile(
EditImageActivity.this,
getPackageName() + ".fileprovider", //(use your app signature + ".provider" )
file1);
mPhotoEditorView.getSource().setImageURI(imageUri);
Log.e("imageUri", " : " + imageUri);
}
}
}
@Override
public void onFailure(@NonNull Exception exception) {
hideLoading();
showSnackbar("Failed to save Image");
}
});
} catch (IOException e) {
e.printStackTrace();
hideLoading();
showSnackbar(e.getMessage());
}
}
如果继续操作不起作用,请尝试通过