Python 在抓取网页时避免重复单词
我从列表(我的df转换为包含重复单词的列表的列)中提取元素,然后将结果返回到df中。我需要找到一种方法,在刮取时排除重复项(以减少时间),但同时,如果是重复项,我需要填充所有重复单词的导出值。 例如: 这是通过使用my_列中的关键字一个接一个地获得的,不会避免重复。 是否有使用逻辑,以便在重复的情况下,仅在刮取中使用第一个值,但在结果列中填充每个关键字的结果 这是我的密码Python 在抓取网页时避免重复单词,python,web-scraping,Python,Web Scraping,我从列表(我的df转换为包含重复单词的列表的列)中提取元素,然后将结果返回到df中。我需要找到一种方法,在刮取时排除重复项(以减少时间),但同时,如果是重复项,我需要填充所有重复单词的导出值。 例如: 这是通过使用my_列中的关键字一个接一个地获得的,不会避免重复。 是否有使用逻辑,以便在重复的情况下,仅在刮取中使用第一个值,但在结果列中填充每个关键字的结果 这是我的密码 for keyword in final_list: for index, row in
for keyword in final_list:
for index, row in data_splitted2.iterrows():
if keyword == row['my_column']:
if keyword == None:
break
# print(keyword)
link = website + 'search/q?name=' + keyword
driver.get(link)
time.sleep(5)
try:
status = driver.find_element_by_class_name("yyyyy")
row['result'] = status.text
except NoSuchElementException:
pass
最后一点,在我的最终df中,我需要保留重复的关键字,以便在刮取时传递它们,但在我的最终df中存在
非常感谢
`如果我理解正确,您可能正在寻找以下内容。它非常简单,只是为了回应特定的问题: 假设您有以下数据帧:
data = ['string1', 'string2', 'string3', 'string2', 'string1', 'string4']
result = ['','','','','','']
df = pd.DataFrame(columns=["my_column",'result'])
df['my_column'],df['result'] = data,result
我们可以在执行操作时跳过重复项,但将这些操作的结果分配给所有行,包括重复项:
for val in df.my_column.unique():
state = "Yes" if random.randint(1,2)==1 else "No"
#in your actual code, the above line will probably have to be replaced with status.text
df.loc[df['my_column'] == val, 'result'] = state
df
随机输出:
my_column result
0 string1 No
1 string2 Yes
2 string3 Yes
3 string2 Yes
4 string1 No
5 string4 No
非常感谢你@我很高兴它对你有用!
my_column result
0 string1 No
1 string2 Yes
2 string3 Yes
3 string2 Yes
4 string1 No
5 string4 No