这是我的小程序测试部分(将在以后使用中)
main.py
import requests
import re
import os
import random
from bs4 import BeautifulSoup
from flask import Flask, request, abort
#from imgurpython import ImgurClient
from argparse import ArgumentParser
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
from apple_news import apple_newss
a=input("plz:")
if a == "applenews":
content0 = apple_newss_content0
content1 = apple_newss_content1
content2 = apple_newss_content2
print (content0)
print (content1)
print (content2)
和我的子程序
apple_news.py
def apple_newss( apple_newss_content0, apple_newss_content1, apple_newss_content2):
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
if a['href']!= None and a['href'].startswith('https://'):
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)
print("\n")
random_list = [] #create random list if needed..
random.shuffle(list_links) #random shuffle the list
apple_newss_content0 = ''
apple_newss_content1 = ''
apple_newss_content2 = ''
for i in range(3): # specify range (5 items in this instance)
res = list_links.pop(random.randint(1, len(list_links))) # pop of each item randomly based on the size of the list
random_list.append(res)
#print(res)
#print(random_list)
print("\n")
apple_newss_content0=random_list[0]
apple_newss_content1=random_list[1]
apple_newss_content2=random_list[2]
我不知道该如何退货
apple_newss_content0
apple_newss_content1
apple_newss_content2
到main.py
(出于某种目的,它必须采用分隔形式,实际上是3个链接)
并让main.py
分别打印这3个
(这个问题我已经坚持了一个月.....)
谢谢!
答案 0 :(得分:0)
如果我了解您的问题,您可以简单地做
def apple_newss():
apple_newss_content0 = 0
apple_newss_content1, apple_newss_content2 = 1, 2
return apple_newss_content0, apple_newss_content1, apple_newss_content2
然后在main.py中:
content0, content1, content2 = apple_newss()
例如
>>> def apple_newss():
... apple_newss_content0 = 0
... apple_newss_content1, apple_newss_content2 = 1, 2
... return apple_newss_content0, apple_newss_content1, apple_newss_content2
...
>>> content0, content1, content2 = apple_newss()
>>> content0, content1, content2
(0, 1, 2)
>>> print(content0)
0
>>> print(content1)
1
>>> print(content2)
2