python - Beautifulsoup - 网络爬虫的问题

标签 python python-3.x python-2.7 beautifulsoup web-crawler

  1. 如何正确输出本新闻网站的所有链接? (以列表形式)

  2. 列表输出后,如何随机返回结果(一次3~5个链接)

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

div class="abdominis rlby clearmen"

我需要这种东西里面的每一个链接

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

谢谢!!代码如下:

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')

#can output all links but with useless information
contents = soup.select("div[class='abdominis rlby clearmen']")[0].find_all('a')
print(contents)

#can output single link but not in list form
#contents = soup.select("div[class='abdominis rlby clearmen']")[0].find('a').get('href')
#print(contents)

最佳答案

这是一个解决方案,如果它包含在指定的 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)  

关于python - Beautifulsoup - 网络爬虫的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49174794/

相关文章:

python - 为什么你可以在字符串上重载 __radd__ 而不是 __rmod__ ?

python - 在 Ubuntu 中为 Python 3.1.2 安装 Pygame

python - 为什么我的代码给出了错误的变量值?

python - 使用Python解压二进制文件仅返回一个值

python-2.7 - pip升级pyzmq时gcc失败

python - 在Python 3和Python 2中处理CSV中的非UTF8字符

python - 跨多个版本创建 conda 包

python - 使用 3d 数组的索引来填充 4d 数组

python - 我如何迭代表并打印列的值 Selenium Webdriver Python

python - 'float' 对象没有属性 '__getitem__' Python 错误