Das Python -Skript funktioniert aus der Befehlszeile, fällt jedoch aus, wenn sie von C# in IIS gerufen werdenPython

Python-Programme
Anonymous
 Das Python -Skript funktioniert aus der Befehlszeile, fällt jedoch aus, wenn sie von C# in IIS gerufen werden

Post by Anonymous »

Ich habe eine MVC -Anwendung in C# geschrieben und auf IIS gehostet. In meinem C# Code versuche ich, ein Python -Skript aufzurufen, um auf die Scopus -Website zuzugreifen und Benutzerinformationen abzurufen. Das Python -Skript funktioniert perfekt, wenn Sie aus der Befehlszeile ausgeführt werden, aber wenn ich es aus meinem C# -Codus nenne, wirft es einen Fehler aus. Ich bin mit einem Erlaubnisproblem konfrontiert, also erstelle ich manuell einige Ordner (PIP, Python ...) und gebe IIS/Benutzer die Erlaubnis.

Code: Select all

Error in python script: Traceback (most recent call last):
File "C:\inetpub\site\scopus.py", line 15, in 
verify_success(sb)
File "C:\inetpub\site\scopus.py", line 9, in verify_success
sb.assert_element('//span[contains(text(), "Author Search")]', timeout=30)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\base_case.py", line 9428, in assert_element
self.wait_for_element_visible(selector, by=by, timeout=timeout)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\base_case.py", line 8853, in wait_for_element_visible
return page_actions.wait_for_element_visible(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\page_actions.py", line 496, in wait_for_element_visible
timeout_exception(NoSuchElementException, message)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\page_actions.py", line 254, in timeout_exception
raise exc(msg)
seleniumbase.common.exceptions.NoSuchElementException: Message:
Element {//span[contains(text(), "Author Search")]} was not present after 30 seconds!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\inetpub\site\scopus.py", line 24, in 
verify_success(sb)
File "C:\inetpub\site\scopus.py", line 9, in verify_success
sb.assert_element('//span[contains(text(), "Author Search")]', timeout=30)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\base_case.py", line 9428, in assert_element
self.wait_for_element_visible(selector, by=by, timeout=timeout)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\base_case.py", line 8853, in wait_for_element_visible
return page_actions.wait_for_element_visible(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\page_actions.py", line 496, in wait_for_element_visible
timeout_exception(NoSuchElementException, message)
File "C:\Windows\system32\config\systemprofile\AppData\Roaming\Python\Python312\site-packages\seleniumbase\fixtures\page_actions.py", line 254, in timeout_exception
raise exc(msg)
seleniumbase.common.exceptions.NoSuchElementException: Message:
Element {//span[contains(text(), "Author Search")]} was not present after 30 seconds!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\inetpub\site\scopus.py", line 26, in 
raise Exception("Detected!")
Exception:  Detected!

Python -Code

Code: Select all

from seleniumbase import SB
from bs4 import BeautifulSoup
import sys
import json

def verify_success(sb):
sb.assert_element('//span[contains(text(), "Author Search")]', timeout=10)
sb.sleep(3)

userId = sys.argv[1]

with SB(uc=True) as sb:
sb.uc_open_with_reconnect(f"https://www.scopus.com/authid/detail.uri?authorId={userId}", 3)
try:
verify_success(sb)
except Exception:
if sb.is_element_visible('input[value*="Verify"]'):
sb.uc_click('input[value*="Verify"]')
else:
sb.uc_gui_click_captcha()
try:
verify_success(sb)
except Exception:
raise Exception("Detected!")
finally:
page_source = sb.get_page_source()
document = BeautifulSoup(page_source, 'html.parser')
citations_node = document.select_one("div[data-testid='metrics-section-citations-count'] span[data-testid='unclickable-count']")
documents_node = document.select_one("div[data-testid='metrics-section-document-count'] span[data-testid='unclickable-count']")
hindex_node = document.select_one("div[data-testid='metrics-section-h-index'] span[data-testid='unclickable-count']")
name_node = document.select_one("h1[data-testid='author-profile-name'] strong")
institute_node = document.select_one("span[data-testid='authorInstitute']")
scopus_information = {
"CitationsNumber": int(citations_node.text.replace(",", "") if citations_node else "0"),
"Documents": int(documents_node.text if documents_node else "0"),
"HIndex": int(hindex_node.text if hindex_node else "0"),
"Name": name_node.text.strip() if name_node else "",
"Institute": institute_node.text.strip() if institute_node else ""
}
print(json.dumps(scopus_information, indent=4))
C# Code:

Code: Select all

public class ScopusInformation
{
public string? Name { get; set; }
public int? CitationsNumber { get; set; }
public int? Documents { get; set; }
public int? HIndex { get; set; }
public string? Institute { get; set; }
public string? ScopusId { get; set; }
}
public interface IScopusService
{
Task GetArticlesForUser(string userId);
}
public class ScopusServiceUsingPython(ILogger logger) : IScopusService
{
public async Task GetArticlesForUser(string userId)
{
string cmd = "py";
var result2 = await DoCmdAsync(cmd, $"scopus.py {userId}");
ScopusInformation? r2 = JsonSerializer.Deserialize(result2);
return r2;
}
private async Task DoCmdAsync(string cmd, string args)
{
logger.LogWarning("DoCmd in python script {cmd} {args}", cmd, args);
try
{
// First, ensure required modules are installed
await EnsurePythonModulesAsync(cmd, new[] { "seleniumbase", "beautifulsoup4", "pyautogui"  });

var start = new ProcessStartInfo
{
FileName = cmd, // cmd is full path to python.exe
Arguments = args, // args is path to .py file and any cmd line args
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};

using var process = new Process { StartInfo = start };
process.Start();

var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();

await Task.WhenAll(outputTask, errorTask);

string result = await outputTask;
string error = await errorTask;

if (!process.WaitForExit(30000)) // 30 seconds timeout
{
process.Kill();
throw new TimeoutException("Python script execution timed out after 30 seconds.");
}

if (!string.IsNullOrEmpty(error))
{
logger.LogWarning("Error in python script: {error}", error);
}

logger.LogWarning("Result in python script: {result}", result);
return result;
}
catch (Exception ex)
{
logger.LogWarning("Exception in python script: {ex}", ex.ToString());
return "";
}
}

private async Task EnsurePythonModulesAsync(string pythonPath, string[] modules)
{
foreach (var module in modules)
{
logger.LogWarning("Checking Python module: {module}", module);
var checkStart = new ProcessStartInfo
{
FileName = pythonPath,
Arguments = $"-c \"import {module}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};

using var checkProcess = new Process { StartInfo = checkStart };
checkProcess.Start();
if (!checkProcess.WaitForExit(10000)) // 10 seconds timeout
{
checkProcess.Kill();
throw new TimeoutException($"Checking for Python module {module} timed out.");
}

if (checkProcess.ExitCode != 0)
{
logger.LogWarning("Installing missing Python module: {module}", module);
var installStart = new ProcessStartInfo
{
FileName = pythonPath,
Arguments = $"-m pip install {module}",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};

using var installProcess = new Process { StartInfo = installStart };
installProcess.Start();

var outputTask = installProcess.StandardOutput.ReadToEndAsync();
var errorTask = installProcess.StandardError.ReadToEndAsync();

if (await Task.WhenAny(Task.WhenAll(outputTask, errorTask), Task.Delay(300000)) == Task.Delay(300000)) // 5 minutes timeout
{
installProcess.Kill();
throw new TimeoutException($"Installation of Python module {module} timed out after 5 minutes.");
}

string output = await outputTask;
string error = await errorTask;

if (installProcess.ExitCode != 0)
{
throw new Exception($"Failed to install Python module {module}: {error}");
}

logger.LogWarning("Successfully installed Python module: {module}", module);
logger.LogWarning("Installation output: {output}", output);
}
else
{
logger.LogWarning("Python module {module} is already installed.", module);
}
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post