Python Beautifulsoup-webcrawler的问题

Python Beautifulsoup-webcrawler的问题,python,python-3.x,python-2.7,beautifulsoup,web-crawler,Python,Python 3.x,Python 2.7,Beautifulsoup,Web Crawler,如何正确输出此新闻网站上的所有链接?(列表形式) 以列表形式输出后,如何随机返回结果(每次3~5个链接) 注意:我需要的代码从第739行开始(几乎可能会有一点变化,因为它每天都刷新) 我需要这里面的每一个环节 <a href="https://tw.news.appledaily.com/life/realtime/20180308/1310910/> 下面是一个解决方案,如果每个链接包含在指定的div中,它会将其附加到列表中 from bs4 import BeautifulSo

如何正确输出此新闻网站上的所有链接?(列表形式)


  • 以列表形式输出后,如何随机返回结果(每次3~5个链接)

  • 注意:我需要的代码从第739行开始(几乎可能会有一点变化,因为它每天都刷新)

    我需要这里面的每一个环节

    <a href="https://tw.news.appledaily.com/life/realtime/20180308/1310910/>
    

    下面是一个解决方案,如果每个链接包含在指定的div中,它会将其附加到列表中

    from bs4 import BeautifulSoup
    from flask import Flask, request, abort
    import requests
    import re
    import random
    import types    
    target_url = 'http://www.appledaily.com.tw/realtimenews/section/new/'
    print('Start parsing appleNews....')
    rs = requests.session()
    res = rs.get(target_url, verify=False)
    soup = BeautifulSoup(res.text, 'html.parser')
    
    list_links = [] # Create empty list
    
    for a in soup.select("div[class='abdominis rlby clearmen']")[0].findAll(href=True): # find links based on div
        list_links.append(a['href']) #append to the list
        print(a['href']) #Check links
    
    for l in list_links: # print list to screen (2nd check)
        print(l)
    
    创建要返回的随机链接

    import random #import random module
    
    random_list = [] #create random list if needed..
    random.shuffle(list_links) #random shuffle the list
    
    for i in range(5): # specify range (5 items in this instance)
        try:
            res = list_links.pop(random.randint(0, len(list_links))) # pop of each item randomly based on the size of the list
            print(res) #print to screen..
            random)list.append(res) # or append to random_list
        except IndexError:
            pass
    
    您要求返回的最后一次编辑

    在这里,它作为一个函数返回x个随机链接的列表

    def return_random_link(list_, num):
        """ Takes in a list and returns a random amount of items """
        random.shuffle(list_)
    
        random_list = []
    
        for i in range(num):
            try: # try to append to the list
                r = list_.pop(random.randint(0, len(list_)))
                random_list.append(r)
            except IndexError: #except an IndexError (no items
                return random_list # Return the list of items
    
        return random_list
    
    random_list = return_random_link(list_links, 5)
    
    for i in random_list:
        print(i)  
    

    如果希望链接标记不包含其子代,可以清除它们:

    for elm in contents:
        elm.clear()
    
    我想我更感兴趣的是只提取链接,不过:

    contents = [a['href'] for a in contents]
    

    要以随机顺序获得结果,请尝试使用random.shuffle()并一次从重新排列的列表中获取所需的任意多个元素。

    以列表形式输出后,如何随机返回结果(每次3~5个链接)。。。。你能澄清一下这是什么意思吗。。返回到何处?一行代码:
    [a['href']表示汤中的a。选择(“div[class='abdominis rlby clearman']”[0]。find_all(href=True)
    ]@johnashu实际上意味着输出,因为我正在为在线聊天机器人编程,所以它应该是“return”XD最后一次编辑,以便将其包装成一个函数,使代码更漂亮。如果有帮助的话。。请用绿色勾选正确答案:)这将帮助未来用户快速找到答案。请原谅代码res=list_links.pop(random.randint(0,len(list_links))有点小问题。有时会说弹出超出范围,但有时不会(这意味着可能是randint(0或randint)(1)这是因为链接资源吗?很抱歉..我添加了一个
    try
    除了
    块,以在列表迭代用尽后返回列表..现在应该可以了..(函数)我可以问数字0有什么问题吗??
    for elm in contents:
        elm.clear()
    
    contents = [a['href'] for a in contents]