Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 为什么<;ul>;标记不提供任何输出?_Python_Python 2.7_Beautifulsoup - Fatal编程技术网

Python 为什么<;ul>;标记不提供任何输出?

Python 为什么<;ul>;标记不提供任何输出?,python,python-2.7,beautifulsoup,Python,Python 2.7,Beautifulsoup,我尝试了以下代码: import urllib from bs4 import BeautifulSoup url = 'http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-1-0-0-0-0.html' pageurl = urllib.urlopen(url) soup = BeautifulSoup(pageurl) print soup.find('ul',{'class':'div_pag

我尝试了以下代码:

import urllib
from bs4 import BeautifulSoup
url = 'http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-1-0-0-0-0.html'
pageurl = urllib.urlopen(url)
soup = BeautifulSoup(pageurl)
print soup.find('ul',{'class':'div_pages'})
我想阅读标签内的链接,以便打开标签内的下一个链接。
因为每个类别有多个页面。

首先,您需要获取下一个页面的URL,然后可以使用urllib2打开下一个页面

要获取URL,如果URL中存在明确的模式,则可以手动构建它

或者您可以阅读
next
标签来阅读内容

# the advantage of using `Next` is it is web text based which is more reliable. 
import urllib
from bs4 import BeautifulSoup
import re
url = 'http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-1-0-0-0-0.html'
pageurl = urllib.urlopen(url)
soup = BeautifulSoup(pageurl)
print soup.find('ul',{'class':'div_pages'}).find(text=re.compile("Next")).find_parent('a')['href']
输出如下所示:

http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-2-0-0-0-0.html
现在你有了下一个页面的链接,如果你想得到下一个,下一个,你只需要重复这个过程。。。页面

让我知道这是否回答了您的问题。

通过对其进行改进,一页一页地阅读下一页:

import re
import urllib
from bs4 import BeautifulSoup


def get_next_page(url):
    pageurl = urllib.urlopen(url)
    soup = BeautifulSoup(pageurl)
    next_text = soup.find('ul', {'class': 'div_pages'}).find(text=re.compile("Next"))
    if next_text:
        return next_text.find_parent('a')['href']
    return None

next_url = 'http://www.freesoft4down.com/Windows/System-Utilities/Clipboard-Tools/Page-1-0-0-0-0.html'
while next_url:
    print 'Retrieving URL {}'.format(next_url)
    next_url = get_next_page(next_url)
您可能希望更改代码,以便实际对页面执行一些有用的操作


例如,您可能希望将
urllib.urlopen
调用放在
while
循环中,以便直接访问页面内容。(为了防止两次检索页面,您不会将URL发送到
获取下一页
功能,而是发送页面的内容。)但这一切都取决于您首先检索这些页面的原因。

代码给出了输出:
你知道如何阅读和打开下一页吗?这很好,但它如何能够自动进入下一页而不必手动重复一个又一个?@wanmohdpayed你真的发明了轮子。如果有任何关于如何收集url的问题,它属于“爬行”的场景,应该指一些工具,如“刮擦”或“坚果”。当你花一些时间弄清楚它的语法时,你会惊讶于它们有多酷。。Scrapy是基于Python的。祝你好运@B.W.先生。我可以根据你的答案抓取一切。谢谢谢谢你,伙计。我刚从你的代码中得到一个想法。我如何在这里显示代码?@wanmohdpayed取决于您为什么要显示代码。有什么不起作用的吗?谢谢@Mark van Lent!我让代码正常工作了。