我在表单的 HTML 中有链接
<a href="/downloadsServlet?docid=abc" target="_blank">Report 1</a>
<a href="/downloadsServlet?docid=ixyz" target="_blank">Fetch Report 2 </a>
Run Code Online (Sandbox Code Playgroud)
我可以使用 BeautifulSoup 获取上述表单的链接列表
我的代码如下
from bs4 import BeautifulSoup
html_page = urllib2.urlopen(url)
soup = BeautifulSoup(html_page)
listOfLinks = list(soup.findall('a'))
Run Code Online (Sandbox Code Playgroud)
但是,我想在引用链接的文本中找到包含“Fetch”一词的链接。
我试过表格
soup.findAll('a', re.compile(".*Fetch.*"))
Run Code Online (Sandbox Code Playgroud)
但这行不通。如何仅选择具有 href 且文本部分中包含“Fetch”一词的标签 a?
写方法:
public int sumRow(int[][] matrix, int row)
Run Code Online (Sandbox Code Playgroud)
row在2D数组中对行进行求和,称为矩阵.
鉴于:
public void run()
{
System.out.println(sumRow(new int[][]{{70,93,68,78,83},{68,89,91,93,72},{98,68,69,79,88}}, 2));
System.out.println(sumRow(new int[][]{{1,1,1}, {2,2,2}, {3,3,3}}, 0));
System.out.println(sumRow(new int[][]{{2,4,6,8,10}, {1,2,3,4,5}, {10,20,30,40,50}}, 2));
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是714,18和78,它们应该是402,3和150.我做错了什么?
这是我的问题。
我知道如何基于 RegEx 创建一个布尔列,如下所示:
df['New Column'] = df.columnA.str.match(regex)
Run Code Online (Sandbox Code Playgroud)
在此示例中,“新列”将包含 True 或 False 值。
但是,如果我想使用一个条件来说明“如果我的 RegEx 返回 true,则推送“这个”值,如果它返回 False,则推送“那个”值,我该怎么做。
感谢您的帮助 :)
我正在尝试在 AWS Lambda 上执行一个 python 函数。在我的函数中,我试图导入 mysql.connector 模块。但是出现了一个错误:
errorMessage": "没有名为 'mysql.connector' 的模块"
我最初在我的 EC2 实例中编写了我的 python 代码。我使用 pip 在我的 python 文件目录中安装了 mysql-connector。
pip install mysql-connector -t /path/to/file/dir
Run Code Online (Sandbox Code Playgroud)
我上传了唯一文件的 zip 文件,而不是任何包含该文件的文件夹。
我在这里创建了一个示例数据框。
df = pd.DataFrame( np.random.randn(10,2), columns=list('AB'))
A B
0 0.459759 0.152645
1 0.183613 0.756527
2 -1.836027 0.032433
3 0.264336 0.170171
4 -0.276347 0.208389
5 0.677709 0.725274
6 -0.547858 0.376683
7 -0.994759 -0.750373
8 0.556593 1.282167
9 -1.444533 0.589768
df['A_rank']= pd.qcut(df['A'],[0,0.25,0.5,0.75,1],duplicates="drop")
A B A_rank
0 0.459759 0.152645 (0.411, 0.678]
1 0.183613 0.756527 (-0.0464, 0.411]
2 -1.836027 0.032433 (-1.837, -0.883]
3 0.264336 0.170171 (-0.0464, 0.411]
4 -0.276347 0.208389 (-0.883, -0.0464]
5 0.677709 0.725274 (0.411, 0.678]
6 -0.547858 0.376683 (-0.883, -0.0464]
7 -0.994759 …Run Code Online (Sandbox Code Playgroud) 我的数据框中有每日股票数据:
Date AAPL NFLX INTC
0 2008-01-02 27.834286 3.764286 25.350000
1 2008-01-03 27.847143 3.724286 24.670000
2 2008-01-04 25.721428 3.515714 22.670000
3 2008-01-07 25.377142 3.554286 22.879999
4 2008-01-08 24.464285 3.328571 22.260000
Run Code Online (Sandbox Code Playgroud)
我想在上面的 df 中使用每个月的最后一天计算每月回报。我猜(在谷歌搜索之后)重新采样是选择当月最后一个交易日的最佳方式。但这似乎不起作用:
df.set_index('Date')
m1= df.resample('M')
print(m1)
Run Code Online (Sandbox Code Playgroud)
得到这个错误:
类型错误:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“Index”的实例
所以我认为这意味着 set_index 不起作用?
我也试过:
df= df.reset_index().set_index('Date')
m1= df.resample('M')
print(m1)
Run Code Online (Sandbox Code Playgroud)
但我收到与上述相同的错误消息。非常感谢您的帮助。
使用变量时出现问题
echo ""| awk '{
x=-0.35
print x^1.35
print -0.35^1.35
}'
Run Code Online (Sandbox Code Playgroud)
结果
+nan
-0.242377
Run Code Online (Sandbox Code Playgroud)
使用 GNU awk
所以我有一个看起来像这样的 df:
Created UserID Service
1/1/2016 a CWS
1/2/2016 a Other
3/5/2016 a Drive
2/7/2017 b Enhancement
... ... ...
Run Code Online (Sandbox Code Playgroud)
我想根据 CWS 和 Drive 的“服务”列中的值对其进行过滤。我是这样做的:
df=df[(df.Service=="CWS") or (df.Service=="Drive")]
Run Code Online (Sandbox Code Playgroud)
它不起作用。有任何想法吗?
给出以下代码:
numbers= [1,2,3,1,2,4,5]
if 5 in numbers:
.................
Run Code Online (Sandbox Code Playgroud)
我们可以注意到我们有一个numbers包含7个项目的list(),我想知道in场景背后的关键字是否在列表中找到匹配项
我开始写
import time
localtime = time.localtime()
print(localtime)
Run Code Online (Sandbox Code Playgroud)
但当地时间给出了九个属性
time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=22,
tm_min=32, tm_sec=41, tm_wday=1, tm_yday=212, tm_isdst=1)
Run Code Online (Sandbox Code Playgroud)
我想通过将小时:分钟:秒与当地时间进行比较来确定Python中的上午还是下午。请建议
python ×8
pandas ×4
dataframe ×3
python-2.7 ×2
awk ×1
aws-lambda ×1
categories ×1
comparison ×1
java ×1
math ×1
nan ×1
python-3.x ×1
regex ×1
time ×1
web-scraping ×1