我尝试使用 Python 和 Selenium 获取某些 HTML 标签的 id。有html代码:
<tr id="10">
<td colspan="5">
<div class="card-view">
<span class="value">PROVIDER_628_54678931</span>
</div>
</td>
</tr>
<tr id="11">
<td colspan="5">
<div class="card-view">
<span class="value">PROVIDER_629_54678932</span>
</div>
</td>
</tr>
<tr id="12">
<td colspan="5">
<div class="card-view">
<span class="value">PROVIDER_730_54678933</span>
</div>
</td>
</tr>
<tr id="13">
<td colspan="5">
<div class="card-view">
<span class="value">PROVIDER_6542_54678934</span>
</div>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
对于仅提取一个父标签的 id,我这样做:
elem = browser.find_element_by_xpath("//span[contains(@class, 'value') and text()='PROVIDER_628_54678931']")
parent = elem.find_element_by_xpath('../../..')
print(parent.get_attribute("id"))
Run Code Online (Sandbox Code Playgroud)
如何在 XPath 中使用正则表达式来获取“span”元素的父 id-s,其中文本包含“PROVIDER_6XX”,但不包含“PROVIDER_7”和 PROVIDER_6542?
我有代码:
static void doSmth() {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add(String.valueOf(i));
}
list.stream().filter("1329"::contains).map(s -> s + "a").forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
为什么我得到:
1a
2a
3a
9a
13a
29a
Run Code Online (Sandbox Code Playgroud)
我期望输出为空,因为列表不包含“ 1329”。
什么样的地图“嗯”?
Map<String,Person> hm;
try (BufferedReader br = new BufferedReader(new FileReader("person.txt")) {
hm = br.lines().map(s -> s.split(","))
.collect(Collectors.toMap(a -> a[0] , a -> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3]))));
Run Code Online (Sandbox Code Playgroud)
它取决于声明吗?
Map<String,Person> hm = new HashMap<>();
Map<String,Person> hm = new TreeMap<>();
Run Code Online (Sandbox Code Playgroud) 有班人:
class Person {
private String id;
private String name;
private int age;
private int amount;
}
Run Code Online (Sandbox Code Playgroud)
我使用外部文件创建了HashMap,Person其中包含以下行:
001,aaa,23,1200
002,bbb,24,1300
003,ccc,25,1400
004,ddd,26,1500
Run Code Online (Sandbox Code Playgroud)
主类
public class Mainclass {
public static void main(String[] args) throws IOException {
List<Person> al = new ArrayList<>();
Map<String,Person> hm = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("./person.txt"))) {
hm = br.lines().map(s -> s.split(","))
.collect(Collectors.toMap(a -> a[0], a-> new Person(a[0],a[1],Integer.valueOf(a[2]),Integer.valueOf(a[3]))));
}
}
Run Code Online (Sandbox Code Playgroud)
}
它适用于HashMap。
怎么做ArraList呢?
我试过了:
al = …Run Code Online (Sandbox Code Playgroud) 有php代码:
\n\n#!/usr/bin/php\n<?php\nif ($file = fopen("file.txt", "r")) {\n while(!feof($file)) {\n $line = fgets($file);\n echo "----" , $line , "----";\n }\n fclose($file);\n}\n?>\nRun Code Online (Sandbox Code Playgroud)\n\n和空文件“file.txt”
\n\n[root@localhost]# ls -l file.txt \n-rw-r--r-- 1 root root 0 \xd0\x90\xd0\xbf\xd1\x80 21 10:14 file.txt\n[root@localhost]# file file.txt \nfile.txt: empty\nRun Code Online (Sandbox Code Playgroud)\n\n为什么我得到?feof 在第一个循环中应该是 true 还是我错了?
\n\n[root@localhost ]# php -f test2.php\n--------\n\n[root@localhost ]# \nRun Code Online (Sandbox Code Playgroud)\n 有一些Java代码:
List<Call> updatedList = updatingUniquedList
.stream()
.map(s -> {
Call call = callsBufferMap.get(s);
}
return call;
}).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果调用变量为空,如何避免添加到最终列表?
我有带有字符串列的 Pandas 数据框,其中包含时间戳为“20000530 172700”如何优雅地将此类字符串更改为“2000-05-30 17:27:00”?数据框包含 > 10k 行。我不想获取每个值,将“-”和“:”插入到指定位置。有没有使用面膜的解决方案?
我想按 ZoneDateTime 字段以相反顺序对列表进行排序,将计数限制为 20
public class VisitViewDTO implements Serializable {
private ZonedDateTime visitDate;
private int count;
public ZonedDateTime getVisitDate() {
return visitDate;
}
}
Run Code Online (Sandbox Code Playgroud)
……
List<VisitViewDTO> visitViewDTOList;
visitViewDTOList.stream().sorted(Comparator.comparing(VisitViewDTO::getVisitDate).reversed()).limit(20).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但得到了NPE:
java.lang.NullPointerException: null
at java.time.chrono.ChronoZonedDateTime.compareTo(ChronoZonedDateTime.java:570)
at java.time.chrono.ChronoZonedDateTime.compareTo(ChronoZonedDateTime.java:121)
at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
at java.util.Collections$ReverseComparator2.compare(Collections.java:5178)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:356)
at java.util.TimSort.sort(TimSort.java:220)
at java.util.Arrays.sort(Arrays.java:1512)
Run Code Online (Sandbox Code Playgroud) 有代码:
List<Integer> al = new ArrayList<>() ;
Iterator iterator = al.iterator();
System.out.println(iterator.getClass().getName());
Run Code Online (Sandbox Code Playgroud)
我得到:
java.util.ArrayList$Itr
Run Code Online (Sandbox Code Playgroud)
这是什么意思:“ ArrayList $ Itr”?
我在这一行中得到了什么Iterator实现?
Iterator iterator = al.iterator();
Run Code Online (Sandbox Code Playgroud) java ×6
java-stream ×5
java-8 ×2
lambda ×2
python ×2
collections ×1
collectors ×1
pandas ×1
php ×1
selenium ×1
xpath ×1