Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 爬虫(我自己设计的)获取的URL不能超过10个_Python_Url_Beautifulsoup_Web Crawler_Information Retrieval - Fatal编程技术网

Python 爬虫(我自己设计的)获取的URL不能超过10个

Python 爬虫(我自己设计的)获取的URL不能超过10个,python,url,beautifulsoup,web-crawler,information-retrieval,Python,Url,Beautifulsoup,Web Crawler,Information Retrieval,我编写了一个简单的Python爬虫来从网站获取URL。代码如下: from bs4 import BeautifulSoup import requests as req def get_soup(url): content = req.get(url).content return BeautifulSoup(content,'lxml') def extract_links(url): soup = get_soup(url) a_tags = soup.

我编写了一个简单的Python爬虫来从网站获取URL。代码如下:

from bs4 import BeautifulSoup
import requests as req

def get_soup(url):
    content = req.get(url).content
    return BeautifulSoup(content,'lxml')

def extract_links(url):
    soup = get_soup(url)
    a_tags = soup.find_all('a', class_="kkyou true-load-invoker")
    links  = set(a_tag.get('href') for a_tag in a_tags)
    return links

def set_of_links(url, size):

'''
breadth-first search for article hyperlinks
'''

seen = set()
active = extract_links(start_url)

while active:
    next_active = set()
    for item in active:
        for result in extract_links(item):
            if result not in seen:
                if len(seen) >= size:
                    break
                else:
                    seen.add(result)
                    next_active.add(result)
    active = next_active

    return seen
基本上,我从我指定的起始url中获取一个soup,提取起始url中具有classKKYU true load invoker的所有url,然后以广度优先的方式对我收集的所有url重复该过程。当我看到一定数量的URL时,我停止这个过程

直到几周前,我运行这个程序还没有问题。我可以指定任意数量的URL,它会为我获取它们。我今天刚刚尝试了完全相同的代码,它最多只返回14个URL。例如,如果我要求它获取50个URL,它将只获取10个URL并停止。很明显,这不会是代码的问题,因为我什么都没有更改!我想知道我试图抓取的页面是否使用了某种机制来阻止我抓取“过多”的页面。我试图抓取的页面是(选择任何文章作为起始url)


对此的任何见解都将不胜感激!我是一个完全的网络爬虫新手。

我在尝试用wget下载整个网站时遇到了类似的错误,其余的都被忽略了。但是当我在每次抓取操作之间添加一些睡眠时恢复,可能是因为您正在爬网的站点有一些反bot配置,这只是一个可能的原因:)问题是,
active
有时是一个ampty集,它的计算结果为false并中断您的循环。@t.m.adam事实并非如此。只要检查一下页面的设计,你就会明白为什么。另外,这在两周前还没有发生。@JackWu有趣!我以前从未听说过这种“睡眠”。你能指出一些我可以阅读更多的资源吗?只要谷歌“wget下载整个站点”,你可以找到像-r或-m这样的参数,做睡眠的是-w,再加上一些时间。睡眠(1)很简单,嗯,不太可能解决你的问题,可以试试。现有的爬虫程序如pysipder或scrpy,也可以使用request或bs4,也可以检查他们的想法