【问题标题】:How to actually use loop in beautifulsoup?如何在 Beautifulsoup 中实际使用循环?
【发布时间】:2019-07-21 01:10:19
【问题描述】:

我正在尝试制作一个网络爬虫。我在里面使用了一些循环。该循环在第一个循环中运行良好,但在第二个循环中运行良好。我总是收到这条消息:“在处理上述异常期间,发生了另一个异常”

import requests
from bs4 import BeautifulSoup


result = 
requests.get("http://desaku.bandungkab.go.id/desaonline/")
#This url is the main web, inside this web there are 270 links of 
#other website. I get into that 270 webs and open every article in 
#each 
web
src = result.content
soup = BeautifulSoup(src, 'lxml')
links = soup.find_all('a')
urls = []
for link in links:
    if "www" in link.text:
        url = link.attrs['href']
        urls.append(url)


num1=len(urls)
b=0
while b<num1:
    result2 = requests.get(urls[b])
    src2 = result2.content
    soup = BeautifulSoup(src2, 'lxml')
    links2 = soup.find_all('a')
    urls2 = []
    for link in links2:
        if "selengkapnya" in link.text:
            url2 = link.attrs['href']
            urls2.append(url2)
    b+=1
#the code run well until this part. If i print this, it will result 
#url that take me directly to specific article

num=len(urls2)
i=0
while i<num:
    result2 = requests.get(urls2[i])
    src2 = result2.content
    soup = BeautifulSoup(src2, 'lxml')
    links2 = soup.find_all('a')
    artikel=[]
    isi = link.attrs['href']
    artikel.append(isi)
    print(artikel)
    i+=1

我希望从网站上获取文章的所有链接并将它们放入名为 artikel=[] 的列表中

【问题讨论】:

    标签: python-3.x loops beautifulsoup python-requests web-crawler


    【解决方案1】:

    问题是您在每次循环迭代时都分配给urls2 = []artikel=[],从而有效地替换了列表。在迭代结束时,您有空数组。您可以使用此代码作为开始:

    import requests
    from bs4 import BeautifulSoup
    
    result = requests.get("http://desaku.bandungkab.go.id/desaonline/")
    #This url is the main web, inside this web there are 270 links of
    #other website. I get into that 270 webs and open every article in
    #each web
    
    src = result.content
    soup = BeautifulSoup(src, 'lxml')
    
    urls = []
    urls2 = []
    
    for link in soup.select('a[href]:contains(www)'):
        urls.append(link['href'])
    
    print('Urls:')
    for url in urls:
        print('Downloading {}'.format(url))
        result2 = requests.get(url)
        src2 = result2.content
        soup = BeautifulSoup(src2, 'lxml')
        for link in soup.select('a[href]:contains(selengkapnya)'):
            print('\tFound link {}'.format(link['href']))
            urls2.append(link['href'])
    
    print('Articles:')
    articles = []
    for url2 in urls2:
        print('Downloading {}'.format(url2))
        result2 = requests.get(url2)
        src2 = result2.content
        soup = BeautifulSoup(src2, 'lxml')
        for link in soup.find_all('a[href]'):
            articles.append(link['href'])
    
    print(articles)
    

    打印:

    Urls:
    Downloading http://www.ancolmekar.desa.id
        Found link http://www.ancolmekar.desa.id/first/artikel/423
        Found link http://www.ancolmekar.desa.id/first/artikel/421
        Found link http://www.ancolmekar.desa.id/first/artikel/420
        Found link http://www.ancolmekar.desa.id/first/artikel/419
        Found link http://www.ancolmekar.desa.id/first/artikel/414
        Found link http://www.ancolmekar.desa.id/first/artikel/413
        Found link http://www.ancolmekar.desa.id/first/artikel/412
        Found link http://www.ancolmekar.desa.id/first/artikel/410
        Found link http://www.ancolmekar.desa.id/first/artikel/410
        Found link http://www.ancolmekar.desa.id/first/artikel/100
    Downloading http://www.arjasari.desa.id
        Found link http://www.arjasari.desa.id/first/artikel/180
        Found link http://www.arjasari.desa.id/first/artikel/190
    
    ...and so on.
    

    【讨论】:

    • 我在运行代码后收到这条消息:/usr/bin/python3.6 /home/acer/PycharmProjects/bs1/example8.py Traceback(最近一次调用最后):文件“/home/ acer/PycharmProjects/bs1/example8.py”,第 15 行, 中的链接,用于 soup.select('a[href]:contains(www)'):文件“/usr/lib/python3/dist-packages /bs4/element.py”,第 1451 行,在选择“仅实现以下伪类:nth-​​of-type。”) NotImplementedError:仅实现以下伪类:nth-​​of-type。跨度>
    • 我明白了,我还没有更新我的 bs4 非常感谢,我已经为此工作了一周。但是如果我想获取文章的内容,我只需要更改最后一个代码,对吗?
    • @SaepulAnwar 确保您没有将 urlsurls2 设置为 [] 每次迭代。
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 2020-04-26
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多