我想在visual studio中使用c#運行外部python腳本。我使用像BeautifulSoup模塊,並請求在Visual Studio C中使用像beautifulSoup和請求的Python模塊#
但我收到以下錯誤
No module named requests
早些時候,我收到了BeautifulSoup同樣的錯誤,我添加下面一行到我的python腳本,並解決了這個錯誤
sys.path.append("[Path to Python]\Python\Python35-32\Lib\site-packages")
我在Visual Studio 2015中使用IronPython。是否有反正我可以克服這個錯誤?如果這是不可能的,是否有其他方式在c#環境中運行python腳本(使用上述模塊)。
我試圖使用denfromufa給出的解決方案,但後來我收到以下錯誤
這是我的Python代碼
import sys
import requests
import re
import io
from bs4 import BeautifulSoup
from math import floor
r = requests.get("https://www.google.com/")
data = r.text
soup = BeautifulSoup(data, 'html.parser')
result = []
for item in soup.find_all(attrs={'class' :'something'}):
for m in item.select('a[href^="something"]'):
m1 = m['href'].replace("something","",1)
m2 = re.sub(r'&.*$', "", m1)
m3 = re.sub(r'%3F.*$', "", m2)
m4 = m3.replace("%2F","/")
m5 = m4.replace("%3A",":")
result.append(m5)
result.append(m.get_text())
for image in item.find_all('img'):
k1 = re.sub(r'&cfs.*$',"",image['src'])
k2 = re.sub(r'^https://something.*$',"",k1)
k3 = re.sub(r'.*url=',"",k2)
k4 = re.sub(r'%3F.*$', "", k3)
k5 = k4.replace("%2F","/")
k6 = k5.replace("%3A",":")
k7 = re.sub(r'.*\.gif',"",k6)
result.append(k7)
seen = set()
result_final = []
for item in result:
if item not in seen:
seen.add(item)
result_final.append(item)
result_final = list(result_final)
我的C#代碼如下
using (Py.GIL())
{
dynamic sys = Py.Import("sys");
dynamic requests = Py.Import("requests");
dynamic re = Py.Import("re");
dynamic io = Py.Import("io");
dynamic BeautifulSoup = Py.Import("bs4");
dynamic math = Py.Import("math");
Console.WriteLine(5);
dynamic r = requests.get("https://www.google.com/");
dynamic data = r.text;
dynamic soup = BeautifulSoup.BeautifulSoup(data, "html.parser");
}
我用
var divExp = new { _class = "smoething" };
var item = soup.find_all(Py.kw("class", divExp._class));
我收到結果。但是,當我嘗試實施的項目變量的選擇方法,我是個得到一個錯誤,說Python對象不包含「選擇」
item.select("a[href^='https://www.google.com/']");
最終答案的定義
using (Py.GIL())
{
dynamic sys = Py.Import("sys");
dynamic requests = Py.Import("requests");
dynamic re = Py.Import("re");
dynamic io = Py.Import("io");
dynamic BeautifulSoup = Py.Import("bs4");
dynamic math = Py.Import("math");
Console.WriteLine(5);
dynamic r = requests.get(url);
dynamic data = r.text;
dynamic soup = BeautifulSoup.BeautifulSoup(data, "html.parser");
var divExp = new { _class = "className" };
var item = soup.find_all(Py.kw("class", divExp._class));
dynamic tag = soup.select("a[href^='https://something.com/']");
for (var i = 1; i < item.Length(); i++)
{
// Extrxting the required info using regex
String input = Convert.ToString(item[i]);
string pattern_link = "(.*href=\"https:[\\/][\\/]something.com[\\/]a.php\\?u=)|(&.*)";
string replacement_link = " ";
Regex rgx_link = new Regex(pattern_link);
string result_link = rgx_link.Replace(input, replacement_link);
.
.
.
.
string pattern_link_1 = "(http|https)%.*";
Regex rgx_link_1 = new Regex(pattern_link_1);
Match result_link_1 = rgx_link_1.Match(result_link);
String input_1_1 = Convert.ToString(result_link_1.Value);
result_link_2 = result_link_2.Replace("%2F", "/").Replace("%3A", ":");
}
}
這聽起來有點傻。我使用PythonEngine.ImportModule(「filename.py」)導入文件後如何執行該文件 –
下面是一個簡單示例:https://github.com/pythonnet/pythonnet/blob/master/README。md – denfromufa
你也可以看看嵌入式測試和nPython控制檯項目 – denfromufa