我的脚本获取标题标签并修改此标签并将généraite变为一个变量 我想在主要的python脚本上单独使用该脚本 在辅助脚本上,généraited变量是
print (oname_cleanedup)
我想在我的剧本
上使用这个变量我的辅助脚本代码
# -*- coding: UTF-8 -*-
import subprocess
from bs4 import BeautifulSoup
import requests
import re
import sys
olinks = sys.argv[1]
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537)'}
session = requests.Session()
response = session.get(olinks, headers=headers)
soup = BeautifulSoup(response.content)
oname = soup.find("title")
if oname.text.find('Saison') >= 0:
regexp = r'(.*?\s+-\s+S)aison\s+(\d+)\s+\xc9.*?(\d+)(.*)'
subst = "{title} {season:02d} Ep {episode}"
else:
regexp = r'(.*?\s+-)(\s+)\xc9.*?(\d+)(.*)'
subst = "{title} Ep {episode}"
oname_cleanedup = re.sub(regexp,
lambda m: subst.format(title=m.group(1), season=int(m.group(2)) if m.group(2).find(" ")==-1 else "", episode=m.group(3)),
oname.text)
print(oname_cleanedup)
抱歉,我忘记了信息
我想导入我的脚本
import sys
sys.path.append('files/')
from my script.py import my fonction
juste如何在辅助脚本上定义我的功能
答案 0 :(得分:1)
如果要将args传递给脚本,可以使用subprocess:
from subprocess import check_output
c = check_output(["python","my_script.py","http://stackoverflow.com/questions/28032363/how-to-use-external-code-python-in-python-files#28032363"])
print(c)
request - how to use external code python in python files - Stack Overflow
如果您尝试from my_script import oname_cleanedup
,则会因为没有提供参数而收到错误消息。你应该将它全部放在一个函数中并将url传递给该函数,以便你可以导入它。
def function(olinks):
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537)'}
session = requests.Session()
response = session.get(olinks, headers=headers)
soup = BeautifulSoup(response.content)
oname = soup.find("title")
if oname.text.find('Saison') >= 0:
regexp = r'(.*?\s+-\s+S)aison\s+(\d+)\s+\xc9.*?(\d+)(.*)'
subst = "{title} {season:02d} Ep {episode}"
else:
regexp = r'(.*?\s+-)(\s+)\xc9.*?(\d+)(.*)'
subst = "{title} Ep {episode}"
return re.sub(regexp,lambda m: subst.format(title=m.group(1), season=int(m.group(2)) if m.group(2).find(" ")==-1 else "", episode=m.group(3)),oname.text)
然后:
from myscript import function
oname_cleanedup = function(url)
我怀疑beautifulsoup可以为你的正则表达式做很多事情。