所以我正在使用Selenium和Python进行一些网络抓取,我遇到了问题.我点击"下一步"按钮移动到某个网站的下一页,但是当我到达最后一页时,我需要停止点击它.现在,我这样做的想法就是在try/except语句中使用some_element.click()并等到它给我一个错误,而按钮不再可点击.看来,.click()不会发出任何类型的信号,当按钮无法点击并且不发出任何真或假信号时,它不会抛出错误.
我尝试使用的代码段:
while True:
try:
button = driver.find_element_by_class_name('Next_button')
button.click()
except:
break
Run Code Online (Sandbox Code Playgroud)
还有其他方法吗?谢谢和欢呼.
我需要检查两个列表是否具有相同的元素,但这些相同的元素也必须位于相同的索引位置.
我想出了一个以下丑陋的解决方案:
def check_any_at_same_index(list_input_1, list_input_2):
# set bool value
check_if_any = 0
for index, element in enumerate(list_input_1):
# check if any elements are the same and also at the same index position
if element == list_input_2[index]:
check_if_any = 1
return check_if_any
if __name__ == "__main__":
list_1 = [1, 2, 4]
list_2 = [2, 4, 1]
list_3 = [1, 3, 5]
# no same elements at same index
print check_any_at_same_index(list_1, list_2)
# has same element 0
print check_any_at_same_index(list_1, list_3)
Run Code Online (Sandbox Code Playgroud)
有什么更好的方法可以做到这一点,任何建议?
我有以下 zip 文件结构:
some_file.zip/folder/folder/files.xml
所以我在 zip 文件的子文件夹中有很多 xml 文件。
到目前为止,我已经设法使用以下代码解压了 zip 文件:
import os.path
import zipfile
with zipfile.ZipFile('some_file.zip') as zf:
for member in zf.infolist():
# Path traversal defense copied from
# http://hg.python.org/cpython/file/tip/Lib/http/server.py#l789
words = member.filename.split('/')
path = "output"
for word in words[:-1]:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir, ''): continue
path = os.path.join(path, word)
zf.extract(member, path)
Run Code Online (Sandbox Code Playgroud)
但我不需要提取文件,而是直接从 zip 文件中读取它们。因此,要么读取 for 循环中的每个文件并对其进行处理,要么将每个文件保存在 Python 中的某种数据结构中。是否可以?
我有一组自定义功能以及使用Vectorizers创建的一组功能,在本例中为TfidfVectorizer。
我所有的自定义功能都是简单的np.arrays(例如[0、5、4、22、1])。我正在使用StandardScaler缩放所有功能,正如您在“管道”中通过在“自定义管道”之后调用StandardScaler所看到的那样。问题是,是否有办法缩放我在“ vectorized_pipeline”中使用的矢量化器。在矢量化器上应用StandardScaler似乎不起作用(出现以下错误:“ ValueError:无法居中稀疏矩阵”)。
另一个问题是,在我将所有功能加入FeatureUnion后是否缩放我的所有功能,还是我分别缩放每个功能(在我的示例中,分别在“ pos_cluster”和“ stylistic_features”中调用缩放器而不是调用他们两个都加入之后),这样做的更好方法是什么?
from sklearn.pipeline import FeatureUnion, Pipeline
from sklearn import feature_selection
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
X = ['I am a sentence', 'an example']
Y = [1, 2]
X_dev = ['another sentence']
inner_scaler = StandardScaler()
# classifier
LinearSVC1 = LinearSVC(tol=1e-4, C = 0.10000000000000001)
# vectorizers
countVecWord = TfidfVectorizer(ngram_range=(1, 3), max_features=2000, analyzer=u'word', sublinear_tf=True, use_idf = True, min_df=2, max_df=0.85, lowercase = True)
countVecWord_tags = TfidfVectorizer(ngram_range=(1, 4), max_features= 1000, analyzer=u'word', min_df=2, …Run Code Online (Sandbox Code Playgroud) 我需要合并两个pandas DataFrame,但不仅要合并确切的列值,还要合并近似的值。
例如,我有以下两个DataFrame:
import pandas as pd
d = {'col1': ["a", "b", "c", "d"], 'col2': [3, 4, 66, 120]}
df = pd.DataFrame(data=d)
col1 col2
0 a 3
1 b 4
2 c 66
3 d 120
d2 = {'col1a': ["aa", "bb", "cc", "dd"], 'col2b': [3, 4, 67, 100]}
df2 = pd.DataFrame(data=d2)
col1a col2b
0 aa 3
1 bb 4
2 cc 67
3 dd 100
Run Code Online (Sandbox Code Playgroud)
现在,如果仅将它们连接到col2和col2b列上,则我将仅获得两行,其中列的值完全相同。
pd.merge(df, df2, how='inner', left_on='col2', right_on='col2b')
col1 col2 col1a col2b
0 …Run Code Online (Sandbox Code Playgroud) 我有一个函数可以计算文本中Trigrams的频率.不需要计算语言学知识,我只需要Perl代码的帮助.
这是功能:
sub extract_frequencies {
for( my $i=0; $i<=$#tag; $i++ ) {
$wordtagfreq{"$word[$i]\t$tag[$i]"}++;
$tagfreq{$tag[$i]}++;
}
# count Tag-Trigramm-Frequencies
my @start = ("<s>","<s>");
unshift @tag, @start; # korrigiert
push @tag, "<s>";
for( my $i=2; $i<=$#tag; $i++ ) {
$ngramfreq[3]{"$tag[$i-2]\t$tag[$i-1]\t$tag[$i]"}++;
}
}
Run Code Online (Sandbox Code Playgroud)
我不理解的特定代码点如下:
1)$ngramfreq[3]
哈希上的索引在这里意味着什么?我是否分别计算每个标签?它是钥匙的长度吗?什么是我的结束键(3个不同的标签键?)?
2) $i<=$#tag
是什么$#在Perl是什么意思?
有一段时间没有使用Perl,所以我希望Perl Monks会帮助我.
我有一个以下数据框:
+-----------+----------+----------+
| some_id | one_col | other_col|
+-----------+----------+----------+
| xx1 | 11| 177|
| xx2 | 1613| 2000|
| xx4 | 0| 12473|
+-----------+----------+----------+
Run Code Online (Sandbox Code Playgroud)
我需要添加一个新列,该列基于对第一列和第二列进行的一些计算,即,例如,对于 col1_value=1 和 col2_value=10 需要生成 col1 包含在 col2 中的百分比,因此 col3_value = (1/10)*100=10%:
+-----------+----------+----------+--------------+
| some_id | one_col | other_col| percentage |
+-----------+----------+----------+--------------+
| xx1 | 11| 177| 6.2 |
| xx3 | 1| 10 | 10 |
| xx2 | 1613| 2000| 80.6 |
| xx4 | 0| 12473| 0 |
+-----------+----------+----------+--------------+
Run Code Online (Sandbox Code Playgroud)
我知道我需要为此使用 udf,但是如何根据结果直接添加新的列值?
一些伪代码: …
python ×6
python-2.7 ×2
apache-spark ×1
hash ×1
nlp ×1
pandas ×1
perl ×1
pyspark ×1
scikit-learn ×1
selenium ×1
tagging ×1
web-scraping ×1
zip ×1
zipfile ×1