Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python的网页表中的最后一个超链接_Python_Python 2.7_Selenium_Beautifulsoup - Fatal编程技术网

使用Python的网页表中的最后一个超链接

使用Python的网页表中的最后一个超链接,python,python-2.7,selenium,beautifulsoup,Python,Python 2.7,Selenium,Beautifulsoup,我正在使用Beautifulsoup4解析网页。与Bing的工作原理类似,如果您输入搜索词,它将返回前十次点击,随后在第2页、第3页等列出的后续页面中返回点击。。。查询后返回的第一个页面包含从第2页到最后一页的超链接。例如,我试图确定的正是最后一页(即第87页)的内容 以下是页面中的HTML源代码示例: <tr><td colspan=4 align=left class='uilt'>����� ������� ��������: 3543.<br>����

我正在使用Beautifulsoup4解析网页。与Bing的工作原理类似,如果您输入搜索词,它将返回前十次点击,随后在第2页、第3页等列出的后续页面中返回点击。。。查询后返回的第一个页面包含从第2页到最后一页的超链接。例如,我试图确定的正是最后一页(即第87页)的内容

以下是页面中的HTML源代码示例:

<tr><td colspan=4 align=left class='uilt'>����� ������� ��������: 3543.<br>��������: 1 <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=2">2</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=3">3</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=4">4</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=5">5</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=6">6</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=7">7</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=8">8</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=9">9</a> <a href="/main/search.php?str=&tag=&nopass=&cat=25&page=10">10</a> <br></td></tr>
����� ������� ��������: 3543.
��������: 1
在上面的例子中,我如何确定最后一个链接是第10页?在上面的内容之后还有更多的HTML,因此我不能简单地从HTML代码的末尾分割X个位置


感谢使用生硒,您应该能够做到以下几点:

driver.find_elements_by_css_selector(".uilt a")[-1].text

这将找到最后一个
标记,该标记是类为
uilt
的元素的后代,并返回其文本。无需使用BeautifulSoup。

使用生硒,您应该能够做到以下几点:

driver.find_elements_by_css_selector(".uilt a")[-1].text

这将找到最后一个
标记,该标记是类为
uilt
的元素的后代,并返回其文本。不需要BeautifulSoup。

首先手动搜索html中的链接数。您可以获取该号码直接链接到最后一页。如果您无法通过这种方式找到最后一个页码,那么您可以从每个搜索结果页的最后一页开始爬网。只需遍历所有链接页面{1…10,11…20,},直到到达最后一页,然后执行操作以查找该页面上的最后一个链接。

首先手动搜索html以获取链接数。您可以获取该号码直接链接到最后一页。如果您无法通过这种方式找到最后一个页码,那么您可以从每个搜索结果页的最后一页开始爬网。只需遍历所有链接页{1…10,11…20,},直到到达最后一页,然后执行操作以查找该页上的最后一个链接。

如果您询问如何使用
BeautifulSoup
在提供的HTML中查找最后一个链接,您可以使用:

或者,使用
find()
find_all()


不过,我同意本主题中的其他参与者的观点,即没有必要使用
BeautifulSoup
Selenium本身是一个功能强大的工具,有很多功能。

如果您询问如何使用
BeautifulSoup
在提供的HTML中找到最后一个链接,您可以使用:

或者,使用
find()
find_all()


不过,我同意本主题中的其他参与者的观点,即没有必要使用
BeautifulSoup
Selenium
本身就是一个强大的工具,有很多功能。

您需要使用BeautifulSoup吗?如果您使用的是Selenium,您不应该需要它。我大部分解析都使用Beautifulsoup,只是使用Selenium模拟用户实际输入的特定术语。您需要使用Beautifulsoup吗?如果您使用的是Selenium,您不应该需要它。我的大部分解析都使用Beautifulsoup,只是使用Selenium模拟用户实际输入的特定术语
soup.find('td', class_='uilt').find_all('a')[-1]