在Visual Studio C中使用像beautifulSoup和请求的Python模块#
问题描述:
我想在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", ":");
}
}
答
- 安装CPython的,2.7版本中的一个,在你的.NET项目安装Python.Runtime.DLL 3.4+
pip install pythonnet
- 参考
- 请访问www.python4.net上的教程,嵌入部分。
```
> scriptcs (ctrl-c to exit or :help for help)
> #r "C:\Python\Anaconda3_64b\Lib\site-packages\Python.Runtime.dll"
> using Python.Runtime;
> dynamic bs4;
> using (Py.GIL()) {bs4=Py.Import("bs4");}
> bs4.__file__.ToString()
C:\Python\Anaconda3_64b\lib\site-packages\bs4\__init__.py
> dynamic rq;
> using (Py.GIL()) {rq=Py.Import("requests");}
> dynamic r=rq.get("https://www.google.com/")
> dynamic soup = bs4.BeautifulSoup(r.text,"html.parser");
> soup.ToString()
```
答
为什么不使用HTML敏捷包?这是C#的等价物。
您可以将其导入到您的解决方案。
对于外部Python脚本看到,
https://www.codeproject.com/articles/121374/step-by-step-guidance-of-calling-iron-python-funct
这听起来有点傻。我使用PythonEngine.ImportModule(“filename.py”)导入文件后如何执行该文件 –
下面是一个简单示例:https://github.com/pythonnet/pythonnet/blob/master/README。md – denfromufa
你也可以看看嵌入式测试和nPython控制台项目 – denfromufa