C# - 使用网络浏览器进行抓取时遇到问题
C# - Having problems using web browser for scraping
我一直在尝试制作一个网络抓取工具,以使用 bing 搜索引擎获取所有 pastebin 网址。
我设法通过使用网络浏览器并让 javascript 运行 然后抓取所有源代码来做到这一点。
string attempt = ""
^
我有两个问题。第一个问题是,如果我不写 MessageBox.Show(this.attempt)
行,变量将由于某种原因为空。另一个问题是现在我只有 9 个链接并且它没有像应该的那样下载其他页面。我认为这都是因为 MessageBox.Show(this.attempt)
的事情。
我知道我的代码不是最好的,可能还有很多更好的方法,但我想获得帮助以了解这里发生的事情。
非常感谢
这是我的代码:
private void Scan(Label pages)
{
string regex = @"https:\/\/pastebin.com\/[a-zA-Z0-9]+";
for (int i = 1; i <= Config.Amount_Of_Pages; i++)
{
Parse(i);
MatchCollection matches = Regex.Matches(this.attempt, regex);
MessageBox.Show(this.attempt);
foreach (Match match in matches)
{
Config.List_Of_Urls.Add(match.Value.ToString());
Config.List_Of_Urls = Config.List_Of_Urls.Distinct().ToList();
}
Config.Amount_Of_Pages_Scanned++;
pages.Invoke(new MethodInvoker(delegate { pages.Text = Config.Amount_Of_Pages_Scanned.ToString(); }));
Files.Write_Urls(Config.List_Of_Urls);
}
MessageBox.Show("Done");
}
private void Parse(int i)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("https://www.bing.com/search?q=site%3apastebin.com++email%3apassword&first=" + i);
}
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
var html = wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
this.attempt = html.ToString();
/* ... */
}
- 我更喜欢使用 Selenium 并为您推荐。
- 如果你想获得不同的 url,你应该使用 HashSet 而不是列表。
- 您应该将可选部分
(www\.)?
添加到正则表达式。
- 为了处理重试策略,我更喜欢使用Polly
结果代码为:
RetryPolicy retryPolicy = Policy.Handle<Exception>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
});
string regex = @"https:\/\/(www\.)?pastebin.com\/[a-zA-Z0-9]+";
HashSet<string> sites = new HashSet<string>();
retryPolicy.Execute(() =>
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("https://www.bing.com/search?q=site%3apastebin.com++email%3apassword&first=1");
// We have to wait until the page will download and rendered in the browser.
Thread.Sleep(1000);
foreach (Match match in Regex.Matches(driver.PageSource, regex))
{
sites.Add(match.Value);
}
}
});
我一直在尝试制作一个网络抓取工具,以使用 bing 搜索引擎获取所有 pastebin 网址。
我设法通过使用网络浏览器并让 javascript 运行 然后抓取所有源代码来做到这一点。
string attempt = ""
^
我有两个问题。第一个问题是,如果我不写 MessageBox.Show(this.attempt)
行,变量将由于某种原因为空。另一个问题是现在我只有 9 个链接并且它没有像应该的那样下载其他页面。我认为这都是因为 MessageBox.Show(this.attempt)
的事情。
我知道我的代码不是最好的,可能还有很多更好的方法,但我想获得帮助以了解这里发生的事情。
非常感谢
这是我的代码:
private void Scan(Label pages)
{
string regex = @"https:\/\/pastebin.com\/[a-zA-Z0-9]+";
for (int i = 1; i <= Config.Amount_Of_Pages; i++)
{
Parse(i);
MatchCollection matches = Regex.Matches(this.attempt, regex);
MessageBox.Show(this.attempt);
foreach (Match match in matches)
{
Config.List_Of_Urls.Add(match.Value.ToString());
Config.List_Of_Urls = Config.List_Of_Urls.Distinct().ToList();
}
Config.Amount_Of_Pages_Scanned++;
pages.Invoke(new MethodInvoker(delegate { pages.Text = Config.Amount_Of_Pages_Scanned.ToString(); }));
Files.Write_Urls(Config.List_Of_Urls);
}
MessageBox.Show("Done");
}
private void Parse(int i)
{
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Navigate("https://www.bing.com/search?q=site%3apastebin.com++email%3apassword&first=" + i);
}
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
var html = wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
this.attempt = html.ToString();
/* ... */
}
- 我更喜欢使用 Selenium 并为您推荐。
- 如果你想获得不同的 url,你应该使用 HashSet 而不是列表。
- 您应该将可选部分
(www\.)?
添加到正则表达式。 - 为了处理重试策略,我更喜欢使用Polly
结果代码为:
RetryPolicy retryPolicy = Policy.Handle<Exception>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(30)
});
string regex = @"https:\/\/(www\.)?pastebin.com\/[a-zA-Z0-9]+";
HashSet<string> sites = new HashSet<string>();
retryPolicy.Execute(() =>
{
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("https://www.bing.com/search?q=site%3apastebin.com++email%3apassword&first=1");
// We have to wait until the page will download and rendered in the browser.
Thread.Sleep(1000);
foreach (Match match in Regex.Matches(driver.PageSource, regex))
{
sites.Add(match.Value);
}
}
});